Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

Proper Calculation of Facts/Values from Dimensional Table

Source ( Download Here )

 

Userstory:
Imagine you have big star shema model and important values/facts in dimensional Tables.
And you don't want to mess up your fact table with multiple low cardinality columns.
Question: How to proper handle these dimTable facts/values?

Simplified Example:

Question: How to get DESIRED YearVolume (because by default it gives 2200 (Totals) in each cell)?

 

  • Anonymous's avatar
    Anonymous
    3 years ago

    Solution ( Download Here )

     

     

    DESIRED YearVolume = 
    CALCULATE(
        SUM(dimSalesYearly[YearVolume]),
        KEEPFILTERS(factSales)
    )

     

     

    It could be used as simple pattern for similar cases

     

    Advanced Example Solution:
    If you have a bit more complicated model:

    Sometimes you need fixed Totals as well:

     

    DESIRED YearVolume (Fixed Totals) = 
    VAR isInSc_Prod = ISINSCOPE(dimProduct[idProduct])
    VAR isInSc_Year = ISINSCOPE('Calendar'[Year])
    VAR Result =
        SWITCH(
            TRUE(),
            NOT isInSc_Prod && NOT isInSc_Year, 
                CALCULATE([DESIRED YearVolume],REMOVEFILTERS(dimProduct[idProduct]),REMOVEFILTERS('Calendar'[Year])),
            NOT isInSc_Prod, CALCULATE([DESIRED YearVolume], REMOVEFILTERS(dimProduct[idProduct])),
            NOT isInSc_Year, CALCULATE([DESIRED YearVolume], REMOVEFILTERS('Calendar'[Year])),
            [DESIRED YearVolume]
        )
    
    RETURN Result

     

     

    P.S. I'd appreciate if you support "PowerQuery: Special symbol for previous step name" request feature by your vote here or/and LinkedIn like/repost here.

5 Replies

  • tamerj1's avatar
    tamerj1
    Icon for Community Champion rankCommunity Champion

    Hi Anonymous 

    how about 

    DESIRED YearVolume 2 = 
    SUMX (
        SUMMARIZE ( 
            factSales, dimSalesYearly[idYearProduct], 
            "@Volume", SUM ( dimSalesYearly[YearVolume] ) 
        ),
        [@Volume]  
    )

    ?

    • NotGregDeckler's avatar
      NotGregDeckler
      Regular Visitor

      tamerj1 Anonymous I came up with:

       

      Measure = 
          VAR __idYearProduct = DISTINCT('factSales'[*idYearProduct])
          VAR __Result = SUMX(FILTER(ALL('dimSalesYearly'), [idYearProduct] IN __idYearProduct), [YearVolume])
      RETURN
          __Result

      Or, you know, make the relationship bi-directional would work as well.

       

  • Anonymous's avatar
    Anonymous
    Not applicable

    Solution ( Download Here )

     

     

    DESIRED YearVolume = 
    CALCULATE(
        SUM(dimSalesYearly[YearVolume]),
        KEEPFILTERS(factSales)
    )

     

     

    It could be used as simple pattern for similar cases

     

    Advanced Example Solution:
    If you have a bit more complicated model:

    Sometimes you need fixed Totals as well:

     

    DESIRED YearVolume (Fixed Totals) = 
    VAR isInSc_Prod = ISINSCOPE(dimProduct[idProduct])
    VAR isInSc_Year = ISINSCOPE('Calendar'[Year])
    VAR Result =
        SWITCH(
            TRUE(),
            NOT isInSc_Prod && NOT isInSc_Year, 
                CALCULATE([DESIRED YearVolume],REMOVEFILTERS(dimProduct[idProduct]),REMOVEFILTERS('Calendar'[Year])),
            NOT isInSc_Prod, CALCULATE([DESIRED YearVolume], REMOVEFILTERS(dimProduct[idProduct])),
            NOT isInSc_Year, CALCULATE([DESIRED YearVolume], REMOVEFILTERS('Calendar'[Year])),
            [DESIRED YearVolume]
        )
    
    RETURN Result

     

     

    P.S. I'd appreciate if you support "PowerQuery: Special symbol for previous step name" request feature by your vote here or/and LinkedIn like/repost here.

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Anonymous ,

       

      Thank you for your sharing. Please kindly accept your workaround as the solution. Then more people will see your post.

       

      Best Regards,
      Rico Zhou

       

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