Forum Discussion

k_h_s's avatar
k_h_s
Frequent Visitor
8 months ago
Solved

Issue to translate sql logic to dax

  Hi everyone, I’m working on a Power BI model where I need to replicate a two-step SQL pattern involving two fact tables and would like guidance on the best DAX approach. Model overview A dime...
  • Rufyda's avatar
    8 months ago

    Hi k_h_s 

    et filtered EntityIDs from FactTable_A

    FilteredEntities =
    CALCULATETABLE(
    VALUES(FactTable_A[EntityID]),
    -- Add your business filters here
    FactTable_A[SomeColumn] = "SomeValue"
    )


    Filter FactTable_B using these EntityIDs

    FilteredFactB =
    FILTER(
    FactTable_B,
    FactTable_B[EntityID] IN FilteredEntities
    && FactTable_B[OtherStaticFilter] = "SomeValue"
    )


    Get latest row per EntityID + Attribute

    LatestFactB =
    FILTER(
    FilteredFactB,
    RANKX(
    FILTER(FilteredFactB,
    FactTable_B[EntityID] = EARLIER(FactTable_B[EntityID]) &&
    FactTable_B[Attribute] = EARLIER(FactTable_B[Attribute])
    ),
    FactTable_B[Date],
    ,
    DESC,
    DENSE
    ) = 1
    )

    Try these solutions and let me know if you need further clarification

    Regards,
    Rufyda Rahma | MIE

     

  • rajendraongole1's avatar
    8 months ago

    Hi k_h_s  - as per above mentioned, information do not remove filter context from FactTable_A.

    you can try the below measue:

     

    Latest FactB Date =
    VAR EntityList =
    VALUES ( FactTable_A[EntityID] )
    RETURN
    CALCULATE (
    MAX ( FactTable_B[Date] ),

    -- Static filters on FactTable_B
    FactTable_B[OtherStaticFilter] = "SomeValue",

    -- Apply Entity list from FactTable_A (SQL IN equivalent)
    TREATAS (
    EntityList,
    FactTable_B[EntityID]
    )
    )

     

    Hope this helps.