Forum Discussion

shuhn1229's avatar
shuhn1229
Resolver I
1 year ago
Solved

Optimizing DAX Logic

Hi all,

 

I am working with a large semantic model with about 10M records. The data quality is not the best.

 

My data model involves cars and car parts (imaginary). The grain for my fact table is on the parts level. The problem is for my column of interest, with the grain of parts (preferred) values are frequently blank. When it is blank, I want logic to pull the value from the dimension table, that is related on the car level (less desired, but better than blank). I have following measure devised for this:

 

VAR SelectedAnalysisType = SELECTEDVALUE('Total Inventory'[Type])
VAR SelectedCategory = SELECTEDVALUE('Total Inventory'[Hoods], BLANK())
VAR SelectedType = SELECTEDVALUE(Cars[Hoods])

RETURN
    IF (
        SelectedAnalysisType = "Pontiac",
        IF (
            SelectedTissueCategory = BLANK(),
            SelectedType,
            SelectedCategory
        ),
        BLANK()
    )

 

However, this measure is very slow when I add it to a table visual, and can take up to 10 seconds to load 10s of thousands of records. Any ideas how to optimize this? The rest of my data model is lightning fast. Another idea is using mQuery but is less desired, since it would likely require a join and I prefer to keep these tables separate.

 

  • shuhn1229 

    If the fallback logic (SelectedType when SelectedCategory is blank) is static, consider adding a calculated column in the fact table. This will avoid runtime computation:

    PreferredCategory =
    IF (
    ISBLANK('Total Inventory'[Hoods]),
    RELATED(Cars[Hoods]),
    'Total Inventory'[Hoods]
    )

    Then, in your measure, directly reference this column instead of recalculating it:

    Optimized Measure with Column =
    VAR SelectedAnalysisType = SELECTEDVALUE('Total Inventory'[Type])
    RETURN
    IF (
    SelectedAnalysisType = "Pontiac",
    SELECTEDVALUE('Total Inventory'[PreferredCategory]),
    BLANK()
    )

    πŸ’Œ If this helped, a Kudos πŸ‘ or Solution mark would be great! πŸŽ‰
    Cheers,
    Kedar
    Connect on LinkedIn

4 Replies

  • shuhn1229 

    If the fallback logic (SelectedType when SelectedCategory is blank) is static, consider adding a calculated column in the fact table. This will avoid runtime computation:

    PreferredCategory =
    IF (
    ISBLANK('Total Inventory'[Hoods]),
    RELATED(Cars[Hoods]),
    'Total Inventory'[Hoods]
    )

    Then, in your measure, directly reference this column instead of recalculating it:

    Optimized Measure with Column =
    VAR SelectedAnalysisType = SELECTEDVALUE('Total Inventory'[Type])
    RETURN
    IF (
    SelectedAnalysisType = "Pontiac",
    SELECTEDVALUE('Total Inventory'[PreferredCategory]),
    BLANK()
    )

    πŸ’Œ If this helped, a Kudos πŸ‘ or Solution mark would be great! πŸŽ‰
    Cheers,
    Kedar
    Connect on LinkedIn

  • Want to mention I tried using GPT for this with limited success and I cannot get the COALESCE function working well with this for some reason.

  • Greg_Deckler's avatar
    Greg_Deckler
    Community Champion

    shuhn1229 Try:

    VAR SelectedAnalysisType = SELECTEDVALUE('Total Inventory'[Type])
    
    RETURN
        IF (
            SelectedAnalysisType = "Pontiac",
            IF (
                SelectedTissueCategory = BLANK(),
                SELECTEDVALUE(Cars[Hoods]),
                SELECTEDVALUE('Total Inventory'[Hoods], BLANK())
            ),
            BLANK()
        )