Power Query - Nested IF statement - Between two values

rythmic_flow

New Member
Joined
Jan 16, 2019
Messages
17
Hello,

I need help writing an IF statement for Power Query using the custom column function.

Column1 has values ranging from 1-12:

1
2
3
4
5
6
7
etc.

I would like to create another column that sorts the values into the following:

0-2
3-5
6-8
9-10
10+

What formula could i enter here to obtain this result?
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Power Query:
let
    Source = Table.FromColumns({List.Accumulate({1..20},{}, (s,c)=> s & {Number.RoundUp(Number.RandomBetween(0,12))})}),
    bins = {2,5,8,10},
    labels = {"0-2", "3-5", "6-8", "9-10", ">10"},
    Result = Table.AddColumn(Source, "Bin", each let c = List.Count(List.Select(bins, (x)=> [Column1]>x)) in labels{c} )
in
    Result
 
Upvote 0
Just to showcase a way that's 100% UI.
It's possible to append the table that holds the bin definition and the lower limit of the value and use sorting on the value before filling down.
To restore the original sort, you can use an index column.

I've re-used the source step from @JGordon11. Those function constructions of yours are clever J.
Power Query:
let
    Source = Table.FromColumns({List.Accumulate({1..20},{}, (s,c)=> s & {Number.RoundUp(Number.RandomBetween(0,12))})}),
    #"Added Index" = Table.AddIndexColumn(Source, "Index", 1, 1, Int64.Type),
    #"Renamed Columns" = Table.RenameColumns(#"Added Index",{{"Column1", "Value"}}),
    #"Appended Query" = Table.Combine({#"Renamed Columns", Bins}),
    #"Sorted Rows" = Table.Sort(#"Appended Query",{{"Value", Order.Ascending}}),
    #"Filled Down" = Table.FillDown(#"Sorted Rows",{"Bin"}),
    #"Filtered Rows" = Table.SelectRows(#"Filled Down", each ([Index] <> null)),
    #"Sorted Rows1" = Table.Sort(#"Filtered Rows",{{"Index", Order.Ascending}}),
    #"Removed Columns" = Table.RemoveColumns(#"Sorted Rows1",{"Index"})
in
    #"Removed Columns"
 
Upvote 0

Forum statistics

Threads
1,223,974
Messages
6,175,739
Members
452,667
Latest member
vanessavalentino83

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top