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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
rashidanwar
Helper III
Helper III

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)

@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

5 REPLIES 5
rashidanwar
Helper III
Helper III

@Greg_Deckler intelligent!
Thank you, so much. 

@rashidanwar Thanks! 🙂


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Greg_Deckler
Super User
Super User

@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)

 


@ 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!:
The Definitive Guide to Power Query (M)

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)

@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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