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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

How can I conditionally replace values in a table query?

I have a table with two date columns, and a calculated column that calculates the number of working days between the value in each column

 

Date 1Date 2Working Days
2/5/20212/6/20214

 

 

I want to conditionally replace the values of the working days column afterwards using the criteria that if the date of both of the first two columns falls within the first 8 days of the month, it will correct the value to 0

 

 

Date 1Date 2Working Days
2/5/20212/6/20210

 

 

 

The issue I am struggling with is how to correct the function below. I believe my else may be incorrect. The else should be to just leave the value as is

 



= if Date.Day([Latest TM Action Date]) < 9 & Date.Day([Latest Submission Date]) < 9
then Table.ReplaceValue(#"Month Start Check", each #"Changed TM Process Days to number", 0, Replacer.ReplaceValue, {"TM Process Days"})
else #"Changed TM Process Days to number"

Alternatively, I can also simply change the values in both date columns... But again, this would require conditional correction. if both dates are within the first 8 days of month, then correct both columns so that both dates are first day of month

2 ACCEPTED SOLUTIONS
CNENFRNL
Community Champion
Community Champion

@Anonymous , you are one step away from your goal,

 

= Table.ReplaceValue(#"Preceding Step", each [TM Process Days], each if Date.Day([Latest TM Action Date]) < 9 and Date.Day([Latest Submission Date]) < 9
then 0 else [TM Process Days], Replacer.ReplaceValue, {"TM Process Days"})

 


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

View solution in original post

Icey
Community Support
Community Support

Hi @Anonymous ,

 

Please check if what @CNENFRNL provided could meet your requirements.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtQ30jdV0oExzZRidaDixvrmMHFjfQuEuAmQowNjWiLETfUNYeJAppFSbCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Latest TM Action Date" = _t, #"Latest Submission Date" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Latest TM Action Date", type date}, {"Latest Submission Date", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "TM Process Days", each Duration.Days([Latest Submission Date]-[Latest TM Action Date])),
    #"Replace Value" = Table.ReplaceValue(#"Added Custom", each [TM Process Days], each if Date.Day([Latest TM Action Date]) < 9 and Date.Day([Latest Submission Date]) < 9
then 0 else [TM Process Days], Replacer.ReplaceValue, {"TM Process Days"})

in
    #"Replace Value"

replace days.JPG

 

 


Alternatively, I can also simply change the values in both date columns... But again, this would require conditional correction. if both dates are within the first 8 days of month, then correct both columns so that both dates are first day of month

 


In addition, for your other needs, please check:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtQ30jdV0oExzZRidaDixvrmMHFjfQuEuAmQowNjWiLETfUNYeJAppFSbCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Latest TM Action Date" = _t, #"Latest Submission Date" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Latest TM Action Date", type date}, {"Latest Submission Date", type date}}),
    #"Replace Value" = Table.ReplaceValue(
    Table.ReplaceValue(
      #"Changed Type",
      each [Latest TM Action Date],
      each 
        if Date.Day([Latest TM Action Date]) < 9 and Date.Day([Latest Submission Date]) < 9 then
          Date.StartOfMonth([Latest TM Action Date])
        else
          [Latest TM Action Date],
      Replacer.ReplaceValue,
      {"Latest TM Action Date"}
    ),
    each [Latest Submission Date],
    each 
      if Date.Day([Latest TM Action Date]) < 9 and Date.Day([Latest Submission Date]) < 9 then
        Date.StartOfMonth([Latest Submission Date])
      else
        [Latest Submission Date],
    Replacer.ReplaceValue,
    {"Latest Submission Date"}
  ),
    #"Added Custom" = Table.AddColumn(#"Replace Value", "TM Process Days", each Duration.Days([Latest Submission Date]-[Latest TM Action Date]))

in
    #"Added Custom"

replace days 2.JPG

 

 

 

Best Regards,

Icey

 

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

2 REPLIES 2
Icey
Community Support
Community Support

Hi @Anonymous ,

 

Please check if what @CNENFRNL provided could meet your requirements.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtQ30jdV0oExzZRidaDixvrmMHFjfQuEuAmQowNjWiLETfUNYeJAppFSbCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Latest TM Action Date" = _t, #"Latest Submission Date" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Latest TM Action Date", type date}, {"Latest Submission Date", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "TM Process Days", each Duration.Days([Latest Submission Date]-[Latest TM Action Date])),
    #"Replace Value" = Table.ReplaceValue(#"Added Custom", each [TM Process Days], each if Date.Day([Latest TM Action Date]) < 9 and Date.Day([Latest Submission Date]) < 9
then 0 else [TM Process Days], Replacer.ReplaceValue, {"TM Process Days"})

in
    #"Replace Value"

replace days.JPG

 

 


Alternatively, I can also simply change the values in both date columns... But again, this would require conditional correction. if both dates are within the first 8 days of month, then correct both columns so that both dates are first day of month

 


In addition, for your other needs, please check:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtQ30jdV0oExzZRidaDixvrmMHFjfQuEuAmQowNjWiLETfUNYeJAppFSbCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Latest TM Action Date" = _t, #"Latest Submission Date" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Latest TM Action Date", type date}, {"Latest Submission Date", type date}}),
    #"Replace Value" = Table.ReplaceValue(
    Table.ReplaceValue(
      #"Changed Type",
      each [Latest TM Action Date],
      each 
        if Date.Day([Latest TM Action Date]) < 9 and Date.Day([Latest Submission Date]) < 9 then
          Date.StartOfMonth([Latest TM Action Date])
        else
          [Latest TM Action Date],
      Replacer.ReplaceValue,
      {"Latest TM Action Date"}
    ),
    each [Latest Submission Date],
    each 
      if Date.Day([Latest TM Action Date]) < 9 and Date.Day([Latest Submission Date]) < 9 then
        Date.StartOfMonth([Latest Submission Date])
      else
        [Latest Submission Date],
    Replacer.ReplaceValue,
    {"Latest Submission Date"}
  ),
    #"Added Custom" = Table.AddColumn(#"Replace Value", "TM Process Days", each Duration.Days([Latest Submission Date]-[Latest TM Action Date]))

in
    #"Added Custom"

replace days 2.JPG

 

 

 

Best Regards,

Icey

 

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

CNENFRNL
Community Champion
Community Champion

@Anonymous , you are one step away from your goal,

 

= Table.ReplaceValue(#"Preceding Step", each [TM Process Days], each if Date.Day([Latest TM Action Date]) < 9 and Date.Day([Latest Submission Date]) < 9
then 0 else [TM Process Days], Replacer.ReplaceValue, {"TM Process Days"})

 


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors