Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Microsoft is giving away 50,000 FREE Microsoft Certification exam vouchers. Get Fabric certified for FREE! Learn more

Reply
oakfootballclub
Helper IV
Helper IV

how to write frequency DAX with high performance

I have the following data:

table: fact_table

year week id value
2023 w1 E01 163
2023 w1 E02 35
2023 w1 E03 123
2023 w1 E04 8
2023 w1 E06 177
2023 w1 E07 38
2023 w2 E01 103
2023 w2 E02 180
2023 w2 E03 31
2023 w2 E04 57
2023 w2 E06 88
2023 w2 E07 179
2023 w2 E08 69
2023 w3 E02 177
2023 w3 E03 176
2023 w3 E04 62
2023 w3 E06 92
2023 w3 E07 163
2023 w4 E01 160
2023 w4 E02 73
2023 w4 E03 38
2023 w4 E04 47
2023 w4 E06 64
2023 w4 E07 152
2023 w4 E08 129
2024 w1 E02 63
2024 w1 E03 111
2024 w1 E04 44
2024 w1 E06 45
2024 w1 E07 175
2024 w2 E01 75
2024 w2 E02 154
2024 w2 E03 60
2024 w2 E04 21
2024 w2 E06 57
2024 w2 E07 153
2024 w2 E08 175
2024 w3 E02 94
2024 w3 E03 172
2024 w3 E04 75
2024 w3 E06 101
2024 w3 E07 99
2024 w4 E01 145

and table id:

id

E01

E02

...

E10

and I want to calculate the frequency of Top 3 value of each week of each id in each, for example,

in year 2023, in week w1, Top 3 is: E06,E01,E03
in week w1, Top 3 is:E02,E07,E01
in week w1, Top 3 is:E02,E03,E07
in week w1, Top 3 is:E01,E07,E08

so  week top3 frequency of E01 is 3, 

that of E02 is 1,

that of E03 is 2,

that of E04 is 0,

........

do you know how to caluculate it in DAX with high performance? Thank you.

 

1 ACCEPTED SOLUTION
Irwan
Super User
Super User

Hello @oakfootballclub 

 

please check if this accomodate your need (not sure if this DAX with high performace).

Irwan_4-1719984377018.png

 

1. Create rank or index based on 'value' column in fact_table so you can easily find top 3.

Rank =
var _Year = 'fact_table'[year]
var _Week = 'fact_table'[week]
Return
RANKX(
    FILTER(
        'fact_table',
        'fact_table'[year]=_Year&&
        'fact_table'[week]=_Week
    ),
    'fact_table'[value],,
    DESC
)
Irwan_5-1719984860338.png
 
2. Create new table with summarize
Year =
var _t1 = SUMMARIZE('fact_table','fact_table'[year])
var _t2 = SUMMARIZE('fact_table','fact_table'[week])
var _rank = SELECTCOLUMNS({(1),(2),(3)},"Rank",[Value])
Return
GENERATE(
    _t1,
    GENERATE(
        _t2,_rank
    )
)
 
_rank is used for creating rank number 1, 2, and 3 since you want to see top 3 (if you need more such as top 4, you can add the value).
 
3. in summarize table, create calculated column for calculating top 3 ID and value based on exact same year, exact same week, and exact same rank.
ID =
MAXX(
    FILTER(
        'fact_table',
        'fact_table'[year]='Year'[year]&&
        'fact_table'[week]='Year'[week]&&
        'fact_table'[Rank]='Year'[Rank]
    ),
    'fact_table'[id]
)
Irwan_3-1719984281156.png
 
Value =
MAXX(
    FILTER(
        'fact_table',
        'fact_table'[year]='Year'[year]&&
        'fact_table'[week]='Year'[week]&&
        'fact_table'[Rank]='Year'[Rank]
    ),
    'fact_table'[value]
)
Irwan_1-1719984218636.png

 

4. in table ID, create measure to count frequency of top 3 ID

Count =
var _ID = SELECTEDVALUE('ID'[ID])
Return
CALCULATE(COUNTROWS(RELATEDTABLE('Year')),'Year'[ID]=_ID)
 
5. Create year and week slicer (value in slicer should be taken from summarized table since no table-relationship made)
Irwan_6-1719985441089.png
 
6. Create Table visual contain of year, week, ID, and Value (all value in table should be taken from summarized table since no table-relationship made)
Irwan_7-1719985575034.png
 
7. Create Table visual contain of ID and Count which is previously measured. In this table, go to Format -> "Edit Interaction" then remove relationship between this table and week slicer (based on your explaination, I assumed you only want to see top 3 frequency ID in annual stage). By removing this interaction, this table will not get filtered when selecting week slicer.
Irwan_8-1719985623320.png

 

Hope this will help you.
Thank you.

View solution in original post

1 REPLY 1
Irwan
Super User
Super User

Hello @oakfootballclub 

 

please check if this accomodate your need (not sure if this DAX with high performace).

Irwan_4-1719984377018.png

 

1. Create rank or index based on 'value' column in fact_table so you can easily find top 3.

Rank =
var _Year = 'fact_table'[year]
var _Week = 'fact_table'[week]
Return
RANKX(
    FILTER(
        'fact_table',
        'fact_table'[year]=_Year&&
        'fact_table'[week]=_Week
    ),
    'fact_table'[value],,
    DESC
)
Irwan_5-1719984860338.png
 
2. Create new table with summarize
Year =
var _t1 = SUMMARIZE('fact_table','fact_table'[year])
var _t2 = SUMMARIZE('fact_table','fact_table'[week])
var _rank = SELECTCOLUMNS({(1),(2),(3)},"Rank",[Value])
Return
GENERATE(
    _t1,
    GENERATE(
        _t2,_rank
    )
)
 
_rank is used for creating rank number 1, 2, and 3 since you want to see top 3 (if you need more such as top 4, you can add the value).
 
3. in summarize table, create calculated column for calculating top 3 ID and value based on exact same year, exact same week, and exact same rank.
ID =
MAXX(
    FILTER(
        'fact_table',
        'fact_table'[year]='Year'[year]&&
        'fact_table'[week]='Year'[week]&&
        'fact_table'[Rank]='Year'[Rank]
    ),
    'fact_table'[id]
)
Irwan_3-1719984281156.png
 
Value =
MAXX(
    FILTER(
        'fact_table',
        'fact_table'[year]='Year'[year]&&
        'fact_table'[week]='Year'[week]&&
        'fact_table'[Rank]='Year'[Rank]
    ),
    'fact_table'[value]
)
Irwan_1-1719984218636.png

 

4. in table ID, create measure to count frequency of top 3 ID

Count =
var _ID = SELECTEDVALUE('ID'[ID])
Return
CALCULATE(COUNTROWS(RELATEDTABLE('Year')),'Year'[ID]=_ID)
 
5. Create year and week slicer (value in slicer should be taken from summarized table since no table-relationship made)
Irwan_6-1719985441089.png
 
6. Create Table visual contain of year, week, ID, and Value (all value in table should be taken from summarized table since no table-relationship made)
Irwan_7-1719985575034.png
 
7. Create Table visual contain of ID and Count which is previously measured. In this table, go to Format -> "Edit Interaction" then remove relationship between this table and week slicer (based on your explaination, I assumed you only want to see top 3 frequency ID in annual stage). By removing this interaction, this table will not get filtered when selecting week slicer.
Irwan_8-1719985623320.png

 

Hope this will help you.
Thank you.

Helpful resources

Announcements
PBIApril_Carousel

Power BI Monthly Update - April 2025

Check out the April 2025 Power BI update to learn about new features.

Notebook Gallery Carousel1

NEW! Community Notebooks Gallery

Explore and share Fabric Notebooks to boost Power BI insights in the new community notebooks gallery.

April2025 Carousel

Fabric Community Update - April 2025

Find out what's new and trending in the Fabric community.

Top Kudoed Authors