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 September 15. Request your voucher.

Reply
Anonymous
Not applicable

how to use the if loop to add new column (power M)

Dear friend,

 

How to use the if loop to add a new column like this info.

I want a new column looking for other columns but in the row is not null if null will look for a higher row looping like below but i don't want write long till -60

 

if [Column5]<> null
then [Column5]
else if #"Replaced Value" [Column5]{[Index]-1} <> null
then #"Replaced Value" [Column5]{[Index]-1}
else if #"Replaced Value" [Column5]{[Index]-2} <> null
then #"Replaced Value" [Column5]{[Index]-2}
else if #"Replaced Value" [Column5]{[Index]-3} <> null
then #"Replaced Value" [Column5]{[Index]-3}

 

Thank you

2 REPLIES 2
amitchandak
Super User
Super User

@Anonymous , You can use List.Select or each

 

new column example

let
_ind = [Index]
_Table = Table.Select(#"Replaced Value" , each [Index] <_ind)
_list = List.Select(#"Replaced Value"[Column5] , each _ <> null)
_value = if List.IsEmpty(_list) then _list{0} else null
in
_value

 

List.Reverse can be used in case the it in revrese order

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
Anonymous
Not applicable

Dear @amitchandak 

thank you may i ask for how to use this code i still don't understand let and in

tep_0-1667793635830.png

 

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Kudoed Authors