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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
kfschaefer
Helper IV
Helper IV

Calculate column handling Null values

Need to add 2 columns together, however one of the columns may contain a Null value and if Null the return the original value.

 

what is the proper syntax?  I need to add the Revenue columns and also the Cost columns together and if one is null the return the column that contains an actual value.

Capture1.PNG

 

 

1 ACCEPTED SOLUTION
Sean
Community Champion
Community Champion

@kfschaefer

select the column => Transform tab => Replace Values => Value To Find: null Replace With: 0

 

You may have do this step "Replaced Value" before the "Added Custom" where you add the 2 columns

View solution in original post

8 REPLIES 8
Rickmaurinus
Helper V
Helper V

There's plenty of ways to do it. I like the List.Sum option. But one could also catch the Nulls through conditional-if statements. Or one could also replace null by 0.  Take one that feels comfortable and if the performance is bad, try another. Also check out: 

 

3 Ways to SUM null values in Power Query 

 

Cheers,

Rick

 

--------------------------------------------------

@ me in replies or I'll lose your thread

 

Master Power Query M? -> https://powerquery.how

Read in-depth articles? -> BI Gorilla

Youtube Channel: BI Gorilla

 

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

try this (case sensitive)

 

=if [column 1] = null then [column 2] else [column 1] + [column 2]



* Matt is an 8 times Microsoft MVP (Power BI) and author of the Power BI Book Supercharge Power BI.
Sean
Community Champion
Community Champion

@kfschaefer

select the column => Transform tab => Replace Values => Value To Find: null Replace With: 0

 

You may have do this step "Replaced Value" before the "Added Custom" where you add the 2 columns

Wherever possible I avoid changing data.

Try this instead:

each List.Sum({[ColA], - [ColB], [ColC]})

 

to perform ColA - ColB + ColC

My full line being:

= Table.AddColumn(#"Previous Step Name", "Sum", each List.Sum({[ColA], - [ColB], [ColC]}), Int64.Type)

Very elegant solution - thanks!

This helped me a lot, @lolouk44

Thanks!

Anonymous
Not applicable

Thank you Lolo,

I will give it a try!

Anonymous
Not applicable

Hello,

I have tried this and it worked for the sum column, thank you.

However, I also need to add a custom column that concatenates those same number type columns (plus others that are text type), so for that I need to replace nulls with "" rather than "0", otherwise the custom column captures the "0" as a text, so it doesn't 'ignore' it. I have tried to sort this out (the type mix type problem) by using Text.From, but this function doesn't accept null values, since it returns null as long as one column contains null in that row... unless one of you guys come up with an easy solution I couldn't figure out I think I will up and turn to Excel (after the query) or DAX when I load to the model. Do you know where I could find a guide or in which language use for each case and in which order should the transformations be applied?

Many thanks in advance

Javier

 

 

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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