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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
Karthik50
Frequent Visitor

(Power-Query) Finding min in a column, subtracting it from other values and grouping the difference

Hi,

In Power-Bi's Power-Query editor, I have a year column.

Year
2000
2001
2002
2003
2004


I want to find the minimum of the column (in this case, the year "2000"), subtract this value from each row in the column (in this case, the values are 0,1,2,3,4) and categorize the values into bins (in this case, ❤️ are 1, >=3 and <6 is 2). So:

YearDifference from Minimum Year
20000
20011
20022
20033
20044

 

YearDifference from Minimum YearCategory
200001
200111
200221
200332
200442

 

I tried List.Min but am not able to move ahead. Can you pleae help me how I can do this? 

1 ACCEPTED SOLUTION
BA_Pete
Super User
Super User

Hi @Karthik50 ,

 

The trick to getting the minimum year value is to reference the previous step in your min function. The categorisation of the years is just a simple 'if' statement:

 

Example code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMFCK1QEzDGEMIxjDGMYwUYqNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Year = _t]),
    chgTypes = Table.TransformColumnTypes(Source,{{"Year", Int64.Type}}),
    
// Relevant steps from here =======>
    addDiffFromMin = Table.AddColumn(chgTypes, "DiffFromMinYear", each [Year] - List.Min(chgTypes[Year])),
    addYearCat = Table.AddColumn(
        addDiffFromMin,
        "YearCategory",
        each if [DiffFromMinYear] < 3 then 1
        else if [DiffFromMinYear] < 6 then 2
        else 999
    )
in
    addYearCat

 

Example output:

BA_Pete_0-1705388046121.png

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




View solution in original post

1 REPLY 1
BA_Pete
Super User
Super User

Hi @Karthik50 ,

 

The trick to getting the minimum year value is to reference the previous step in your min function. The categorisation of the years is just a simple 'if' statement:

 

Example code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMFCK1QEzDGEMIxjDGMYwUYqNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Year = _t]),
    chgTypes = Table.TransformColumnTypes(Source,{{"Year", Int64.Type}}),
    
// Relevant steps from here =======>
    addDiffFromMin = Table.AddColumn(chgTypes, "DiffFromMinYear", each [Year] - List.Min(chgTypes[Year])),
    addYearCat = Table.AddColumn(
        addDiffFromMin,
        "YearCategory",
        each if [DiffFromMinYear] < 3 then 1
        else if [DiffFromMinYear] < 6 then 2
        else 999
    )
in
    addYearCat

 

Example output:

BA_Pete_0-1705388046121.png

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Prices go up Feb. 11th.

Feb2025 Sticker Challenge

Join our Community Sticker Challenge 2025

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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