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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Anonymous
Not applicable

IF AND statement Power Query

Hi. I'm trying to add new custom column using IF AND statement. If "Aging" is greater than 60, and "Aging" is less than 365, give "Total PD".

If(and(Aging>60,Aging<365),Total PD,0)

 

It seems like it's giving me "Token Literal Expected" error when I try to put above statement.

 

What am I doing wrong?

5 REPLIES 5
v-lid-msft
Community Support
Community Support

Hi @Anonymous ,


How about the result after you follow the suggestions mentioned in my original post?Could you please provide more details about it If it doesn't meet your requirement?

 

Best regards,

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
v-lid-msft
Community Support
Community Support

Hi @Anonymous ,

 

In Power Query Editor, we can also use the gui to add such a column.

 

7.PNG

8.PNG9.PNG10.PNG

 

The M Query generated by the GUI

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjZQ0lEyNDBQitWJVjJH5hgjeLEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Aging = _t, #"Total PD" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Aging", Int64.Type}, {"Total PD", Int64.Type}}),
    #"Added Conditional Column" = Table.AddColumn(#"Changed Type", "NewColumn", each if [Aging] <= 60 then 0 else if [Aging] >= 365 then 0 else [Total PD]),
    #"Added Conditional Column1" = Table.AddColumn(#"Added Conditional Column", "NewColumn2", each if [Aging] <= 60 then 0 else if [Aging] < 365 then [Total PD] else 0)
in
    #"Added Conditional Column1"

 

 

Although it can not add AND condition, but it can works in most condition.


BTW, pbix as attached.

 

Best regards,

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
rsimonsen
Frequent Visitor

ahjlim32

If you are doing this with Power Query Editor then Gordonlilj solution is the way to go. If you are using the add new column under modeling this should work.

IfTest = if(and([Aging]>60,[Aging]<365),[Total PD],0)

Nathaniel_C
Community Champion
Community Champion

Hi @Anonymous ,

 

Try this:

 

 Table.AddColumn(#"Changed Type", "Custom", each if [Aging] > 60 and [Aging] <365 then [Total PD] else 0)


Let me know if you have any questions.

If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos are nice too.
Nathaniel





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Gordonlilj
Solution Sage
Solution Sage

Hi,

 

Are you creating the column in the power query editor? if so the code you're using will not work as it's using DAX (power query uses M). The code should work if you create a calculated column instead.

 

If you want to create it using a custom column in power query then the code below should work

if [Aging] >60 and [Aging]<365 then [Total PD] else 0

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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