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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
russellbruce
New Member

Replace value in table based off of a greater than statement

I have a table where I want to replace vaules in Collumn A if the vaule is greater than 480 and I want it to change to 480

The vaule in this collumn can not go over 480

In Power Query 

2 ACCEPTED SOLUTIONS
adudani
Super User
Super User

hi @russellbruce ,

use add a  custom column 

 

adudani_0-1675816437662.png

 

 

type this in:

 

if [Collumn A] > 480 then 480 else [Collumn A]

 

click ok.

 

Let me know if this resolves the issue.

 

Appreciate a thumbs up if this is helpful.

Did I answer your question? Mark my post as a solution, this will help others!
If my response(s) assisted you in any way, don't forget to drop me a Kudos 🙂
Kind Regards,
Avinash

View solution in original post

edhans
Super User
Super User

Use this logic:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("LcvBDQAgCAPAXXj7QAsIsxD2X8MEfF1KSyYdXKqVdLe1ptKqe+uMyRjFogXz5Jid/B4xd9v8/5WqHg==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Data = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Data", Int64.Type}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Data", Order.Descending}}),
    #"Replaced Value" = 
        Table.ReplaceValue(
            #"Sorted Rows",
            each if [Data] > 480 then [Data] else false,
            each 480,
            Replacer.ReplaceValue,
            {"Data"}
        )
in
    #"Replaced Value"

If the value is over 480, then it returns the value of the column. If it isn't it evaluates to false and leaves the Table.ReplaceValues function.

 

If it returned the value of the Data field, it replaces it with 480 regardless of what it is (but your first condition said only return it if it is over 480)

So this:

edhans_0-1675816956591.png


Becomes this:

edhans_1-1675816974795.png

 

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.

 



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

2 REPLIES 2
edhans
Super User
Super User

Use this logic:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("LcvBDQAgCAPAXXj7QAsIsxD2X8MEfF1KSyYdXKqVdLe1ptKqe+uMyRjFogXz5Jid/B4xd9v8/5WqHg==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Data = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Data", Int64.Type}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Data", Order.Descending}}),
    #"Replaced Value" = 
        Table.ReplaceValue(
            #"Sorted Rows",
            each if [Data] > 480 then [Data] else false,
            each 480,
            Replacer.ReplaceValue,
            {"Data"}
        )
in
    #"Replaced Value"

If the value is over 480, then it returns the value of the column. If it isn't it evaluates to false and leaves the Table.ReplaceValues function.

 

If it returned the value of the Data field, it replaces it with 480 regardless of what it is (but your first condition said only return it if it is over 480)

So this:

edhans_0-1675816956591.png


Becomes this:

edhans_1-1675816974795.png

 

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.

 



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
adudani
Super User
Super User

hi @russellbruce ,

use add a  custom column 

 

adudani_0-1675816437662.png

 

 

type this in:

 

if [Collumn A] > 480 then 480 else [Collumn A]

 

click ok.

 

Let me know if this resolves the issue.

 

Appreciate a thumbs up if this is helpful.

Did I answer your question? Mark my post as a solution, this will help others!
If my response(s) assisted you in any way, don't forget to drop me a Kudos 🙂
Kind Regards,
Avinash

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

Check out the May 2024 Power BI update to learn about new features.

Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

Top Solution Authors
Top Kudoed Authors