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

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
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,385
Messages
6,171,780
Members
452,424
Latest member
Sheila003

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