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
bikeoholic
Frequent Visitor

Count in SCD2 dimension - optimization

Hello PB community!

 

Can anybody please help me with DAX optimization in this scenario:

I have Tabular model with 10 dimension tables, no real facts. One of the user request is to count units in Units table through time.  This table is a SCD2 table type and only 2 dates in table are valid_from and valid_to. They will use year-month and year attributes on reports and in slicers. Beside date attribute they wan't to use attributes Units Type and Country of Origin.

I did't connect my Calendar table with Units table in Tabular model and I have combined this DAX code:

 

 

 

Cnt of units:=
CALCULATE (
   // DISTINCTCOUNT ( 'Units'[unit_id] ),
   SUMX(VALUES('Units'[unit_id] ),1),
    FILTER (
        'Units',
        COUNTROWS (
            FILTER (
                VALUES ( 'CalendarTable'[Date] ),         
                'CalendarTable'[Date]>= 'Units'[unit_valid_from]
                    && 'CalendarTable'[Date] <= 'Units'[unit_valid_to]
            )
        )
    )
)

 

 

 

So this code is doing some sort of cross join / cross aplly on date table and since Units table isn't really small (it has 500K+ rows) it's slow. Calendar table is from DB Model, not from PB and has data from 2015 to 2025.

Current server timing from dax studio are like this:

In grid: Year Month, Unit type, Country of origin and my metric

Filter: Year in (2020, 2019)

 

cnt_units_timing.png

 

I already replaced DISTINCOUNT with SUMX code. With using DISTINCOUNT function:

cnt_units_timing_distinct.png

 

 

 

 

 

 

 

 

 

 

 

So, are there are ideas how to optimize this code even more? Is there any way I can solve this differently (connections in Tabular with Calendar table, etc..?)

 

Similar problems: https://community.powerbi.com/t5/Desktop/How-to-find-number-of-customers-in-a-SCD2-type-table/m-p/97... 

 

 

1 ACCEPTED SOLUTION
AlB
Super User
Super User

@bikeoholic 

Pending the response to my questions above, try this:

Cnt of units V2 :=
CALCULATE (
    SUMX ( VALUES ( 'Units'[unit_id] ), 1 ),
    VAR minDate_ =MIN ( 'CalendarTable'[Date] )
    VAR maxDate_ = MAX ( 'CalendarTable'[Date] )
    RETURN
        FILTER (
            ALL ( 'Units'[unit_valid_from], 'Units'[unit_valid_to] ),
            NOT ( maxDate_ < 'Units'[unit_valid_from]
                || minDate_ > 'Units'[unit_valid_to] )
        )
)

 

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

SU18_powerbi_badge

View solution in original post

7 REPLIES 7
AlB
Super User
Super User

@bikeoholic 

4 secs is still considerable and it's engaging the FE significantly.  I'm curious if this version will be faster:

Cnt of units V2_B :=
VAR minDate_ =
    MIN ( 'CalendarTable'[Date] )
VAR maxDate_ =
    MAX ( 'CalendarTable'[Date] )
RETURN
    COUNTROWS (
        FILTER (
            ALL ( 'Units'[unit_id], 'Units'[unit_valid_from], 'Units'[unit_valid_to] ),
            NOT ( maxDate_ < 'Units'[unit_valid_from]
                || minDate_ > 'Units'[unit_valid_to] )
        )
    )

 or this:

Cnt of units V2_C :=
VAR minDate_ =
    MIN ( 'CalendarTable'[Date] )
VAR maxDate_ =
    MAX ( 'CalendarTable'[Date] )
RETURN
    COUNTROWS (
        FILTER (
            ALL ( 'Units'[unit_id], 'Units'[unit_valid_from], 'Units'[unit_valid_to] ),
             maxDate_ >= 'Units'[unit_valid_from] && minDate_ <= 'Units'[unit_valid_to]
        )
    )

 

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

SU18_powerbi_badge

bikeoholic
Frequent Visitor

@AlBThank you, your first code works like a charm 😎

Result after your DAX code:

cnt_units_timing_newdax.png

 

In second code I don't get the right results. I didn't had time yet to go in deep why..

 

Best regards, v

@bikeoholic 

The second should do the same as the first. If the results are not right, it probably has to do with the relationships on the model. I made some assumptions, like no relationship between the date table and the Units table, that seemingly do not hold.

 

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

SU18_powerbi_badge

 

bikeoholic
Frequent Visitor

Hi !

 

No, sorry, I cannot share PB file 😞

Yes, metric ofcourse, named "Cnt Count of Units". I didn't wrote down as well that this DAX code gets the job done..it works, but it's slow. I mean 25s is not much, but table will get bigger and bigger.

There are only active relationships in Tabular model. To me it's a straight forward Tabular model. Nothing "fancy". This metric is defined in Tabular and not in PB.

 

Below is an example of few random rows and columns from "Units" table. For each unit there is a valid from and to date. Unit can be valid throuh many months or just 1 month. They can be valid 1 day, but I don't care for day/date level. I have to count 1st row only in Jan 2020, 2nd row in all next months..and so on.

unit_table.png

 

This is an example of a table visualisation on a report:

pb_example.png

 

 

Tnx, I will try your suggestions!

 

AlB
Super User
Super User

@bikeoholic 

Pending the response to my questions above, try this:

Cnt of units V2 :=
CALCULATE (
    SUMX ( VALUES ( 'Units'[unit_id] ), 1 ),
    VAR minDate_ =MIN ( 'CalendarTable'[Date] )
    VAR maxDate_ = MAX ( 'CalendarTable'[Date] )
    RETURN
        FILTER (
            ALL ( 'Units'[unit_valid_from], 'Units'[unit_valid_to] ),
            NOT ( maxDate_ < 'Units'[unit_valid_from]
                || minDate_ > 'Units'[unit_valid_to] )
        )
)

 

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

SU18_powerbi_badge

@bikeoholic 

this could be better depending on the data distribution. It leverages the filters being applied to reduce the number of rows to scan 

Cnt of units :=
VAR minDate_ =
    MIN ( 'CalendarTable'[Date] )
VAR maxDate_ =
    MAX ( 'CalendarTable'[Date] )
RETURN
    COUNTROWS (
        FILTER (
            SUMMARIZE (
                'Units',
                'Units'[unit_id],
                'Units'[unit_valid_from],
                'Units'[unit_valid_to]
            ),
            NOT ( maxDate_ < 'Units'[unit_valid_from]
                || minDate_ > 'Units'[unit_valid_to] )
        )
    )

 

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

SU18_powerbi_badge

AlB
Super User
Super User

Hi @bikeoholic 

I suppose you cannot share the pbix?

Is Cnt of units  a measure? Are you using it with any filters applied? What are the active relationships?

Please explain a bit more what you mean by One of the user request is to count units in Units table through time

Do ee want the units that have both 'Units'[unit_valid_from] and 'Units'[unit_valid_to] within the date period selected??

Best if you can provide an example

 

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

SU18_powerbi_badge

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.

Top Solution Authors