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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

Create a new column in Power BI power query

Hi all,

Presently I have a calculated column in one of my tables.

I did did via the 'normal way' in Power BI: Modeling/ Calculations/ New Column.

Now I want to add a column with the same calculation in power query.

Below is the formula, which I want to use in Power query.

Is this possible and if so, can somebody give me some guidance (as this part of Power BI is new to me🤔)?!

Thanks!, John

 

most recent = 
var thisContainer = 'Kramer_srs_ope_estimate'[container_nr]
var lastDateByContainerNr = 
    CALCULATE(
        MAX(Kramer_srs_ope_estimate[order_dat])
        ,FILTER(
            ALL('Kramer_srs_ope_estimate')
            ,Kramer_srs_ope_estimate[container_nr] = thisContainer
        )
    )
return
IF('Kramer_srs_ope_estimate'[order_dat] = lastDateByContainerNr, "yes", "no")

 

2 ACCEPTED SOLUTIONS
edhans
Super User
Super User

Hi @Anonymous - I am not sure I would recommend this. This may not perform well over large (hundreds of thousands) records, but is workable. See the formula in the Max Date step.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSUTLUN9Q3MjAyUIrViVZKAgoYIQskAwWMkQVAWkzRtZihazGHC8QCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Container Number" = _t, #"Order Date" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Order Date", type date}}),
    #"Max Date" = 
        Table.AddColumn(
            #"Changed Type",
            "Max Date",
            each 
                let
                    varCurrentContainer = [Container Number]
                in
            Table.Max(
                Table.SelectRows(
                    #"Changed Type",
                    each [Container Number] = varCurrentContainer
                ),
                "Order Date"
            )[Order Date]
        )
in
    #"Max Date"

Based on my fake data in the first two columns, it returns the max date for each container.

edhans_0-1597702276532.png

Power Query isn't the best place to do scans of tables. 
Ideally, this would be in a measure, not a calculated column, but I don't know your requirements. Reporting the date in a visual can be done by a measure. If you need the date in a slicer or report filter, then it must go in a column.

 

How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.

 

If I've answered your question, please mark as the solution. If not, can you provide some sample data with expected output?

How to get good help fast. Help us help you.
How to Get Your Question Answered Quickly
How to provide sample data in the Power BI Forum

 



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

View solution in original post

v-xuding-msft
Community Support
Community Support

Hi @Anonymous ,

 

I think @edhans's solution is working perfectly. I create a sample using "Group by" to get the max date. You also could have a try.

3.gif

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WstA31DcyMDJQ0lFyBGJDI6VYHbCoBbKwsQFY2ETfCCbqBFIMEzU0RjHDDCZsjixsCjPZANkMY7iwKbKwpVJsLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, ID = _t, Amount = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"ID", type text}, {"Amount", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"ID"}, {{"Max", each List.Max([Date]), type nullable date}, {"All", each _, type table [Date=nullable date, ID=nullable text, Amount=nullable number]}}),
    #"Expanded All" = Table.ExpandTableColumn(#"Grouped Rows", "All", {"Date", "Amount"}, {"All.Date", "All.Amount"}),
    #"Added Custom" = Table.AddColumn(#"Expanded All", "Custom", each if [Max] = [All.Date] then "yes" else "no")
in
    #"Added Custom"

 

 

Best Regards,
Xue Ding
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

5 REPLIES 5
v-xuding-msft
Community Support
Community Support

Hi @Anonymous ,

 

I think @edhans's solution is working perfectly. I create a sample using "Group by" to get the max date. You also could have a try.

3.gif

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WstA31DcyMDJQ0lFyBGJDI6VYHbCoBbKwsQFY2ETfCCbqBFIMEzU0RjHDDCZsjixsCjPZANkMY7iwKbKwpVJsLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, ID = _t, Amount = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"ID", type text}, {"Amount", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"ID"}, {{"Max", each List.Max([Date]), type nullable date}, {"All", each _, type table [Date=nullable date, ID=nullable text, Amount=nullable number]}}),
    #"Expanded All" = Table.ExpandTableColumn(#"Grouped Rows", "All", {"Date", "Amount"}, {"All.Date", "All.Amount"}),
    #"Added Custom" = Table.AddColumn(#"Expanded All", "Custom", each if [Max] = [All.Date] then "yes" else "no")
in
    #"Added Custom"

 

 

Best Regards,
Xue Ding
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

good day.,

You are really an expert, thanks so much!

I maneged to solve the issue, together with the info of the other advisor.

Thanks again!

John

edhans
Super User
Super User

Hi @Anonymous - I am not sure I would recommend this. This may not perform well over large (hundreds of thousands) records, but is workable. See the formula in the Max Date step.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSUTLUN9Q3MjAyUIrViVZKAgoYIQskAwWMkQVAWkzRtZihazGHC8QCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Container Number" = _t, #"Order Date" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Order Date", type date}}),
    #"Max Date" = 
        Table.AddColumn(
            #"Changed Type",
            "Max Date",
            each 
                let
                    varCurrentContainer = [Container Number]
                in
            Table.Max(
                Table.SelectRows(
                    #"Changed Type",
                    each [Container Number] = varCurrentContainer
                ),
                "Order Date"
            )[Order Date]
        )
in
    #"Max Date"

Based on my fake data in the first two columns, it returns the max date for each container.

edhans_0-1597702276532.png

Power Query isn't the best place to do scans of tables. 
Ideally, this would be in a measure, not a calculated column, but I don't know your requirements. Reporting the date in a visual can be done by a measure. If you need the date in a slicer or report filter, then it must go in a column.

 

How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.

 

If I've answered your question, please mark as the solution. If not, can you provide some sample data with expected output?

How to get good help fast. Help us help you.
How to Get Your Question Answered Quickly
How to provide sample data in the Power BI Forum

 



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
Anonymous
Not applicable

good day @edhans.,

You are really an expert, thanks so much!

I maneged to solve the issue, together with the info of the other advisor.

Thanks again!

John

Great @Anonymous - glad I was able to assist.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.