Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Power-BI-User
New Member

Start and End dates in a type 2 SCD table and DAX for slicer

Hi everyone,

 

I am trying to convert a history revision table into a type 2 SCD table, which can then be used for dynamic slicing through a slider visual. Being new to Power BI and DAX, I am facing some issues when trying to reformat the table.

 

Here's a sample of what my table looks like:

 

versionUIDdateoperationsurrogate keyfirst datelast date
1OBJECT_X26/06/2021CREATE58526/06/202126/06/2021
2OBJECT_X28/06/2021UPDATE155628/06/202128/06/2021
3OBJECT_X28/06/2021UPDATE261028/06/202128/06/2021
4OBJECT_X28/06/2021UPDATE264228/06/202128/06/2021
5OBJECT_X2/07/2021UPDATE32462/07/20212/07/2021
6OBJECT_X2/07/2021UPDATE32762/07/20212/07/2021
7OBJECT_X2/07/2021UPDATE46212/07/20212/07/2021
8OBJECT_X2/07/2021UPDATE46522/07/20212/07/2021
9OBJECT_X15/07/2021UPDATE575115/07/202115/07/2021
10OBJECT_X17/08/2021UPDATE2318517/08/202117/08/2021

 

I got the columns version, UID, date and operation from the data source. Column surrogate key is just an index column created in Power BI. 

 

Column start date is just copying the date column values, using the following DAX code -

 

 

First Date = CALCULATE(MIN('SCD-Test'[date]), ALLEXCEPT('SCD-Test','SCD-Test'[Surrogate Key]))

 

 

I tried to copy the values in date for the same UIDs but with the next version number to fill out last date, but my code didn't work -

 

 

Last Date = CALCULATE(MIN('SCD-Test'[date]), 'SCD-Test'[version] + 1)

 

 

However, this just copied the date column again. What is the proper way to get the dates for the next version numbers for every UID?

 

Also, taking performance and file size into consideration, it would be better to create these columns during the data preprocessing stage instead. The first date column can be created using the GUI, but what would be the M code to create the last date column?

 

The second thing I want to know is how to write the DAX code to get the number of active UIDs in a specific time period. I know the algorithm, but I cannot convert it into DAX.

 

 

Count rows where (the start date is less than or equal to start date on slicer) and (end date is greater than end date on slicer) and (operation is not DELETE)

 

 

One thing that I am struggling with in general is performing operations in DAX to get aggregated stats for every categorical value in a column or based on some condition. I ran the following commands to try and understand the basics, but I didn't get the results I was expecting:

 

 

CALCULATE (COUNTROWS('SCD-Test'), ALLEXCEPT ('SCD-Test', 'SCD-Test'[version]))
CALCULATE (COUNTROWS('SCD-Test'), VALUES('SCD-Test'[version]))

 

 

Both returned the total count of all rows in the data, not the counts segregated by version numbers.

1 ACCEPTED SOLUTION
V-lianl-msft
Community Support
Community Support

Hi @Power-BI-User ,

 

Try to create an index column and then a custom column in the query editor:

= try #"Added Index" [date] { [Index]  + 1 } otherwise null

Get the number of active UIDs in a specific time period:

Measure = CALCULATE(COUNT('Table (3)'[UID]),FILTER('Table (3)','Table (3)'[first date]>=MIN('date'[Date])&&'Table (3)'[last date]<=MAX('date'[Date])||ISBLANK('Table (3)'[last date])))

 

Best Regards,
Liang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
V-lianl-msft
Community Support
Community Support

Hi @Power-BI-User ,

 

Try to create an index column and then a custom column in the query editor:

= try #"Added Index" [date] { [Index]  + 1 } otherwise null

Get the number of active UIDs in a specific time period:

Measure = CALCULATE(COUNT('Table (3)'[UID]),FILTER('Table (3)','Table (3)'[first date]>=MIN('date'[Date])&&'Table (3)'[last date]<=MAX('date'[Date])||ISBLANK('Table (3)'[last date])))

 

Best Regards,
Liang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi Liang,

 

Thank you very much for your help. The DAX code gives me the exact output I want. 🙂

 

Cheers.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.