cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
joao_costa
New Member

Create calculated measure and use it as filter

I have a table with transactions and want to count the transactions by country to then group those and then use as a filter.

 

I got to the transaction count using a measure:

country_count = 

CALCULATE(
    DISTINCTCOUNT('Table'[trx_id]),
    ALLSELECTED('Table'[Country]))
 
My goal is then to group these, say by intervals of 50 up to 200 (then "200+"), and use that as a filter in a slicer.
I am able to use a simple if statement to derive that but then it does not work as a filter in the slicer.
 
Any ideas on how to solve?
TY
 
1 ACCEPTED SOLUTION
v-stephen-msft
Community Support
Community Support

Hi @joao_costa ,

 

Here's my solution.

1.country_count is measure, and is created as 

country_count = CALCULATE(DISTINCTCOUNT('Table'[trx_id]),FILTER(ALLSELECTED('Table'),[Country]=MAX('Table'[Country])))

 

vstephenmsft_1-1636965980228.png

 

2.Create a calculated table using dax

Table 2 = GENERATESERIES ( 1, MAXX('Table',[country_count]))

vstephenmsft_0-1636965962770.png

 

3.Then create a measure, put it in the filter, set show items when the value is 1.

Measure = IF([country_count]<=MAX('Table 2'[Value])&&MIN('Table 2'[Value])<=[country_count],1)

vstephenmsft_2-1636966101044.png

4.The slicer is created using value field from Table 2. No relationship between two tables. The slicer can move the slider to filter.

vstephenmsft_3-1636966134848.png

 

 

Check more details from the attachment.

 

 

Best Regards,

Stephen Tao

 

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

 

 

 

View solution in original post

2 REPLIES 2
v-stephen-msft
Community Support
Community Support

Hi @joao_costa ,

 

Here's my solution.

1.country_count is measure, and is created as 

country_count = CALCULATE(DISTINCTCOUNT('Table'[trx_id]),FILTER(ALLSELECTED('Table'),[Country]=MAX('Table'[Country])))

 

vstephenmsft_1-1636965980228.png

 

2.Create a calculated table using dax

Table 2 = GENERATESERIES ( 1, MAXX('Table',[country_count]))

vstephenmsft_0-1636965962770.png

 

3.Then create a measure, put it in the filter, set show items when the value is 1.

Measure = IF([country_count]<=MAX('Table 2'[Value])&&MIN('Table 2'[Value])<=[country_count],1)

vstephenmsft_2-1636966101044.png

4.The slicer is created using value field from Table 2. No relationship between two tables. The slicer can move the slider to filter.

vstephenmsft_3-1636966134848.png

 

 

Check more details from the attachment.

 

 

Best Regards,

Stephen Tao

 

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

 

 

 

skeets20
Resolver I
Resolver I

Created a Calculation Group with Each of your grouping (and the logic) as calculated items. Then use the calc group as your slicer.

 

calcgroupslicer2.gif

skeets20_1-1636568999366.png

 

The DAX for each item looks like this. I parameterized the bounds so you can use same logic in each item, just change the bounds. Watch out for overlapping items though

 

 

 

VAR _lowerBound = 0
VAR _upperBound = 4
VAR _summary =
    CALCULATETABLE (
        VALUES ( 'txn'[country] ),
        FILTER (
            SUMMARIZE ( 'txn', 'txn'[country], "_count", DISTINCTCOUNT ( 'txn'[Index] ) ),
            [_count] >= _lowerBound
                && [_count] <= _upperBound
        )
    )
RETURN
  CALCULATE(SELECTEDMEASURE(),  _summary)

 

 

 

Helpful resources

Announcements
May 23 2022 epsiode 5 without aka link.jpg

The Power BI Community Show

Welcome to the Power BI Community Show! Jeroen ter Heerdt talks about the importance of Data Modeling.

charticulator_carousel_with_text (1).png

Charticulator Design Challenge

Put your data visualization and design skills to the test! This exciting challenge is happening now through June10th!

Power BI Dev Camp Session 22 without aka link and time 768x460.jpg

Check it Out!

Watch Session 22 Ted's Dev Camp along with past sessions!

Power BI Release May 2022 768x460.png

Check it out!

Click here to read more about the May 2022 updates!

Top Kudoed Authors