2

I have a table where I want to update the values of "Percent Values" Measure at each filter (a user can select either one, or multiple values from a slicer/s). In visualization all the markers for a Year, Month need to add to 1.0 With or Without Slicer selections.

With the help of @Phil Leh (Issues with implementing grouped percentage with ALLEXCEPT in PowerBI) measure works on Year, Month and the measure gives correct values:

enter image description here

enter image description here

However I am not sure what I can do to have % values update with multiple selections. For example, here the percent values didn't calculate to 1.0 for Race and Year, Month at each Year, Month: enter image description here

I made change to existing DAX (that gives correct values based on Year, Month) so that it can capture Race as well. But the results are wrong.

Percent Values = 
VAR num =
   CALCULATE( SUM ( question[Count Values] ))

-- this one filters month and year
VAR fmy =
    FILTER (
        -- clear all filter from our table
        ALL ( question ),
        -- and keep only the values matching the
        -- filter context's current month and year
        question[Date].[Month]
            = MAX ( question[Date].[Month] )
            && question[Date].[Year]
                = MAX ( question[Date].[Year] )
            --|| question[Race]
            --    = MAX ( question[Race] )
            --|| question[Gender]
            --    = MAX( question[Gender] ) 
            --|| question[visa_country]
            --    = MAX( question[visa_country] ) 
    )

VAR fr =
    FILTER (
        -- clear all filter from our table
        ALL ( question ),
        -- and keep only the values matching the
        -- filter context's current month and year
         question[Race]
                = MAX ( question[Race] )
    )

VAR denom =
    IF (
        -- checking if Month is in current filter context
        ISINSCOPE ( question[Date].[Month] ), --|| ISINSCOPE ( question[Race] ) || ISINSCOPE ( question[Gender] ) || ISINSCOPE (question[visa_country] ),
           
        -- if yes, use the filter fmy (calculate denom for this quarter
        CALCULATE (
            SUM ( question[Count Values] ),
            ALLSELECTED (),
            fmy
        ),
        --IF (
        --        ISINSCOPE ( question[Race] ),
        --        CALCULATE (
        --            SUM ( question[Count Values] ),
        --            ALLSELECTED (),
        --           fr
        --        ),
            -- else, calculate the denom for all values
            -- this could also be BLANK() or some other calculation
        --    CALCULATE (
        --        SUM ( question[Count Values] ),
        --        ALLSELECTED ()
        --    )
        --)
        CALCULATE (
                SUM ( question[Count Values] ),
                ALLSELECTED ()
            )
    )

RETURN
    DIVIDE (
        num,
        denom
    )

In the above photo, the % values aren't being recalculated.

Here is the data in .csv format:

Race,Gender,visa_type,Count Values,visa_country,Processing Type,Date
White,Female,C,1,Canada,Custodial,2/14/2014 0:00
Other,Male,M,5,Mexico,Express,1/20/2015 0:00
Hispanic,Male,R,6,Russia,Refugee,2/18/2014 0:00
White,Female,B,4,Brazil,Asylum,3/7/2014 0:00
Hispanic,Male,C,1,Canada,Refugee,4/11/2015 0:00
White,Female,R,7,Russia,Custodial,4/23/2014 0:00
White,Male,M,9,Mexico,Express,4/1/2014 0:00
Hispanic,Male,B,3,Brazil,Refugee,4/13/2014 0:00
White,Female,R,1,Russia,Express,7/31/2014 0:00
White,Male,C,7,Canada,Asylum,9/6/2015 0:00
White,Female,M,2,Mexico,Express,7/22/2014 0:00
Black,Female,B,5,Brazil,Custodial,8/13/2014 0:00
White,Male,R,1,Russia,Asylum,12/9/2014 0:00
White,Female,M,4,Mexico,Asylum,12/6/2014 0:00
Black,Female,B,6,Brazil,Express,12/13/2015 0:00
White,Male,C,7,Canada,Refugee,10/26/2014 0:00
Hispanic,Female,R,5,Brazil,Refugee,9/6/2015 0:00
White,Male,B,9,Mexico,Asylum,9/6/2015 0:00

.xlsx file here: https://github.com/TestingGround00/powerbi_question/blob/main/input_data_table.xlsx

I have made another DAX that now gives 100% in whole subset, but it really needs to add in 100% for each Year, Month With and/or without Slicers.

Sum Value = SUM ( Table[Count Values] ) 

% Value = 
DIVIDE ( 
    [Sum Value],
    CALCULATE ( [Sum Value], ALLSELECTED ( ) )
)

The out put I need is this: enter image description here enter image description here

I have looked at these questions, however they were somewhat irrelevant:

  1. Dynamic measure that updates based on slicer selection
  2. Power BI - Dynamic measure based on slicer selection
  3. PowerBI How to compute Dynamic measure based on slicer selection?

Without Slicer selections I needed this output-

enter image description here enter image description here

However, with the measure from Smpa01's post Without Slicer data changes-

enter image description here enter image description here

WannabeSmith
  • 435
  • 4
  • 18

2 Answers2

1

If I understood the question correctly, you were hoping for a measure to return the following

S1

S2

You can achieve that with a very simple measure like this

Measure =
VAR _numerator =
    CALCULATE ( SUM ( question[Count Values] ) )
VAR _denominator =
    CALCULATE ( SUM ( question[Count Values] ), ALLSELECTED () )
RETURN
    DIVIDE ( _numerator, _denominator )

There are few things to remember when you are dealing with Dates in DAX. a. Use a Calendar Table and bring Year, Month Name slicers from that table. b. Don't use the date hierarchy from the fact table unless you absolutely need to.

Edit

This is what it looks like without any selection

S3

smpa01
  • 4,149
  • 2
  • 12
  • 23
  • Hi smpa01! Thank you for your help. This achieves what I was looking for except i would need 1.0 at each Year, Month if no selections have been made in the slicers (for instance, it adds 0.08, 0.11, 0.4 for 2014 April without selection). I believe I won't be able to create a Date Table since I don't have all the dates. But such table need to them to be present. Likewise, there are holes in the table (I tried to mimic it in my sample set above). – WannabeSmith Dec 19 '21 at 17:13
  • I did not understand what you meant by `except i would need 1.0 at each Year, Month if no selections have been made in the slicers (for instance, it adds 0.08, 0.11, 0.4 for 2014 April without selection)`. I have added a screenshot of the table without any selection – smpa01 Dec 19 '21 at 17:28
  • Sorry about that. I edited my question. Please find the compare and contrast table on the bottom of my question. All in all, if the data is filtered (with Slicer selection/s), the group needs to add up to 1.0 at each month and year. Without it, it also needs to add to 1.0. I am trying to mimic the % stack graph in power bi. However, line graph doesn't have a percent variation of that so I have to make a DAX. – WannabeSmith Dec 19 '21 at 18:03
  • I think I got it. May I ask if you can review my post below? You are certainly expert so you would be able to see any pitfall (and help me see how I could have worded my question). – WannabeSmith Dec 19 '21 at 21:50
0

Thank you @smpa01 for your help. I apologize if I've miscommunicated anywhere. But this did the trick and this is what I was looking for:

Create a Sum measure:

Sum Value = SUM ( question[Count Values] )

Then create a % value measure where you calculate on values:

% Value = 
DIVIDE (
    [Sum Value],
    CALCULATE (
        [Sum Value],
        ALLSELECTED (),
        VALUES ( question[Date].[Year] ),
        VALUES (  question[Date].[Month] )
    )
)

enter image description here

enter image description here

enter image description here

WannabeSmith
  • 435
  • 4
  • 18