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
decarsul
Helper IV
Helper IV

Power Pivot, help with binning / grouping

Hi there!

 

I need some help for work, as i seem to be having trouble binning / grouping values to make it visually better in an excel driven dashboard using power query and power pivot. (yes thats right, not Power BI Desktop due to publication limitations)

 

I currently have a fact table with data, containing a column with a datediff to calculate how many worksdays an order has taken to process. Now i want to group them with labels i have in a dimension table containing a high and low, so that i can later add or remove labels easily.

 

Now i have tried using the if function, which becomes very large and not very easily editable. I have tried the switch function, but don't seem to be able to make that one work, so i turn to you!

 

Fact table layout: (fictious data)

Unique IDbegindateenddateworkdays
tk00011-1-20202-1-20202
tk00022-1-20204-1-20202
tk00033-1-20206-1-20202
tk00044-1-20208-1-20203
tk00055-1-202010-1-20205
tk00066-1-202012-1-20205
tk00077-1-202014-1-20206
tk00088-1-202016-1-20207
tk00099-1-202018-1-20207
tk001010-1-202020-1-20207
tk001111-1-202022-1-20208

 

Dimension table layout:

labellowhigh
<1 workday01
1 workday12
2 workdays23
3 workdays34
4 workdays45
1 week510
2 weeks1015
3 weeks1520
4 weeks2025

 

How would i combine the 2, to create a column in the fact table with the values mentioned in the dim table?

1 ACCEPTED SOLUTION
mahoneypat
Employee
Employee

The solution from Ricardo is a clever way to do it in M.  If you want to do it in DAX, here is an expression you can use in a calculated column to get your desired result.

 

label = var currentworkdays = 'Fact'[workdays]
return CALCULATE(MIN('Dimension'[label]), 'Dimension'[low]<=currentworkdays, 'Dimension'[high]>=currentworkdays)
 
If this works for you, please mark it as the solution.  Kudos are great too.  Please let me know if it doesn't or if any questions.
Regards,
Pat




Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

3 REPLIES 3
mahoneypat
Employee
Employee

The solution from Ricardo is a clever way to do it in M.  If you want to do it in DAX, here is an expression you can use in a calculated column to get your desired result.

 

label = var currentworkdays = 'Fact'[workdays]
return CALCULATE(MIN('Dimension'[label]), 'Dimension'[low]<=currentworkdays, 'Dimension'[high]>=currentworkdays)
 
If this works for you, please mark it as the solution.  Kudos are great too.  Please let me know if it doesn't or if any questions.
Regards,
Pat




Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


camargos88
Community Champion
Community Champion

Hi @decarsul ,

 

You can try this code for a new column:

 

let _workDays = [workdays] in
Table.SelectColumns(Table.SelectRows
(tb_group, each
_workDays >= [low] and
_workDays < [high]), "label")

 

Capture.PNGCapture 1.PNG

 

Did I answer your question? Mark my post as a solution!
Ricardo



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



@camargos88 i like your way of thinking, however i didn't have the networkdays calculated thru powerM query's unfortunately. Briefly looked into it and couldn't figure it out. So even tho this would be the smarter choice, i've taken the DAX route suggested by @mahoneypat which worked like a charm and solved my issue!

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.

Top Solution Authors
Top Kudoed Authors