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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
rashidanwar
Advocate II
Advocate II

Creating dynamic categories using DAX

Hi everyone,

I have a problem that is more mathetical infact. I would appreciate if someone helps.

I have a table as shown below 

orderline_id             order_id            qty
1                              1                        3
2                              1                        2
3                              1                        1
1                              2                        6
2                              2                        5
1                              3                        9
2                              3                        11

I need to create a 4th Column that will be the qunatity group column based on the SUM of qty for each order_id. Each group will be based on the following logic.
SUM of qty Column 1-9, then category would be 1 
SUM of qty Column 10-18, then category would be 2
SUM of qty Column 19-27, then category would be 3
and so on 

Forexample the above table would like as below.
orderline_id             order_id            qty          qty_group
1                              1                        3              1
2                              1                        2              1
3                              1                        1              1
1                              2                        6              2
2                              2                        5              2
1                              3                        9              3
2                              3                        11            3

In case of order_id 1 the sum of Qty would be 6 so it lies in the range of 1-9 so the Qty_group would be 1
In case of order_id 2 the sum of Qty would be 11 so it lies in the range of 10-18 so the Qty_group would be 2
In case of order_id 3 the sum of Qty would be 20 so it lies in the range of 19-27 so the Qty_group would be 3

Actually we need to calculate the qty Group dynamically, bcause the Sum of Qty for an individual order id can go as high as 1600.

Looking fo

1 ACCEPTED SOLUTION

@rashidanwar Try the alternate formula I posted:

Measure = 
    VAR __qty = SUM('Table'[Column1])
    VAR __Mod = MOD(__qty,9)
RETURN
    IF(__Mod = 0, DIVIDE(__qty,9), TRUNC(DIVIDE(__qty,9))+1)


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

5 REPLIES 5
rashidanwar
Advocate II
Advocate II

@Greg_Deckler intelligent!
Thank you, so much. 

@rashidanwar Thanks! 🙂



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
Greg_Deckler
Community Champion
Community Champion

@rashidanwar Try a measure like this:

 

Measure =
  VAR __qty = SUM('Table'[qty])
RETURN
  SWITCH(TRUE(),
    __qty < 10,1
    __qty < 19,2
    3
  )

or try this:

Measure = 
    VAR __qty = SUM('Table'[Column1])
    VAR __Mod = MOD(__qty,9)
RETURN
    IF(__Mod = 0, DIVIDE(__qty,9), TRUNC(DIVIDE(__qty,9))+1)

 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Thank you @Greg_Deckler.
You are right and I am doing already this. Problem is that there are alot of orders and sum of qunatity can go up to 1600 for a single order, that is why a static measure would not be a good option. There should be some way to increment the logic. 
for example value paramter of switch function is icremented by 9 and the result parameter is incremented by 1.

Some kind of looping is refquired here.

If I go using static logic then you see that I just extended your solution to 10 categories and we still reach at the value of 91, and 1600 is too far away.

SWITCH(TRUE(),
    __qty < 10,1
    __qty < 19,2
    __qty < 28,3
    __qty < 37,4
    __qty < 46,5
    __qty < 55,6
    __qty < 64,7
    __qty < 73,8
    __qty < 82,9
    __qty < 91,10
    ......
  )

 

@rashidanwar Try the alternate formula I posted:

Measure = 
    VAR __qty = SUM('Table'[Column1])
    VAR __Mod = MOD(__qty,9)
RETURN
    IF(__Mod = 0, DIVIDE(__qty,9), TRUNC(DIVIDE(__qty,9))+1)


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Top Solution Authors