Monday, 28 May 2018

Visualize work in progress(WIP) at any given point of time in Power BI


It is a very common requirement to visualize the WIP or sales at any given point of time in Power BI. This task sound really simple but it needs extensive DAX effort to summaries this details.



A simple example.


Incident No
Incident Open Date
Incident Closed Date
2018 Jan
2018 Feb
2018 Mar
1ABC
01/01/2018
02/02/2018
1
1
0
2ABC
01/01/2018
30/01/2018
1
0
0
3ABC
15/01/2018
15/03/2018
1
1
1
4ABC
06/02/2018

0
1
1
Total
3
3
2


To do this task, first create the DATE table which has Year and Month columns.
In your data table you use to measure the WIP create a measure like below:

Measure =
 CALCULATE(
DISTINCTCOUNT(Workload[INCIDENTNO]),
GENERATE(VALUES(Dates[TIMELINE_DATE]),
FILTER(Workload,CONTAINS(
    DATESBETWEEN(Dates[TIMELINE_DATE],Workload[CREATED_DATE],Workload[CLOSED_DATE]),
                Dates[TIMELINE_DATE],Dates[TIMELINE_DATE])
)
)
)




****Most importantly this measure works only when your Workload table has NO relationship with Dates table. ****

DAX commands used in above measure are: 

CALCULATE(<expression>,<filter1>,<filter2>…)
DISTINCTCOUNT(<column>)
GENERATE(<table1>, <table2>)
VALUES(<TableNameOrColumnName>)
FILTER(<table>,<filter>)
CONTAINS(<table>, <columnName>, <value>[, <columnName>, <value>]…)
DATESBETWEEN(<dates>,<start_date>,<end_date>)

Wednesday, 9 May 2018

Add/Remove SQL DB instance in SQL Server

There may be situations that you need to add a new DB instance to one of your production DB's or remove an instance with the purpose of relocation DB's.

Adding and instance or removing an instance is very straight forward if you follow the installation set up wizard. Also you can use below links to refer for knowledge.

Remove:
https://docs.microsoft.com/en-us/sql/sql-server/install/uninstall-an-existing-instance-of-sql-server-setup?view=sql-server-2017

Add feature:
https://docs.microsoft.com/en-us/sql/database-engine/install-windows/add-features-to-an-instance-of-sql-server-setup?view=sql-server-2017

However most expensive question is whether the DB server need a restart after adding or removing an instance. What is the knocked on effect on production DB server. I have done this many times in my life and you can add or remove an instance without a DB restart. There is no impact to the other instances on the SQL server. However i would recommend taking a snapshot before performing any action to mitigate any risks.

Hope this answers!

Dateadd command in SQL

This is very much straight forward but easily forgettable. "dateadd" command is a very useful command in queries. That's why i thought of adding this to my blog.


select dateadd(week,-3,getdate());
select dateadd(day,-21,getdate());



Ex:

select  * from dbo.tblMembership
where ValidTill >dateadd(week,-3,getdate());




How to find which tables have specific column names in SQL Server

There are times you are required to find a specific column name and what tables are linked up with similar column names. This is easier with third party tools available however if your organisation does not support any third party tool you might think this is more like a manual work. Of course not! Below is the query to get you there. Try and see :)

SELECT t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE c.name LIKE '%avail%'
ORDER BY schema_name, table_name;


Creating a user and grant read access to table level in SQL server


There are some occasions that you need to give limited permissions such as limiting permission in table level. Let say if you grant DB level rad access this permission cascade down to entire DB. However what if you want to grant read access only to few tables? Below i describe an easy way to create a SQL user or windows login and grant table level read access permission.

Launch SQL server management studio.

To create a new user collapse Security -> Right click on User -> Select New user. This way you can create a SQL User account


If you need to give access to domain user select the Windows authentication and follow the next steps.



Go to server roles and make sure Public user role has been granted


Go to User Mapping and select the DB and make sure Public Role membership has been selected


Click Ok to complete the user creation.
Open a new Query window and choose your DB and run the below query.

Template:
GRANT SELECT ON "dbo"."tablename1" TO "<domain>\<username>"
GRANT SELECT ON "dbo"." Tablename2" TO "<domain>\<username>"

GRANT SELECT ON "dbo"." tablename1" TO "<SQL username>"
GRANT SELECT ON "dbo"." Tablename2" TO "<SQL username>"

Example:

GRANT SELECT ON "dbo"."Account" TO "sqlusername"
GRANT SELECT ON "dbo"."Branch" TO "sqlusername"
GRANT SELECT ON "dbo"."Business" TO "sqlusername"


To see how the permissions are now showing, go to DB and collapse Security, then expand Users. Select the user you created then right click and select Properties. If you go to Securables it will show you the granted permission like below.