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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
nirvana_moksh
Impactful Individual
Impactful Individual

Fill with Previous Value

Hello All,

 

Below is the sample data that I have at hand. The data in large consists of Head --> Branch --> Branch Section which can be seen as the hierarchy even. What I am trying to achieve for the data after sorting it by the said hierarchy is to fill the blanks by the previous quarter value. I was kinda successful by first sorting the data in Power Query, replacing all blanks by null and then filling down both Amount 1 and Amount 2 column which worked for most but not for all. Also, this would work not as intended if Q1 itself is blank and is being populated by value of the above group:

 

HEADBRANCHBRANCH SECTIONAMOUNT 1AMOUNT 2QUARTER YEAR
A1BRANCH 1T1 2456Q1 2017
A2BRANCH 1T1  Q2 2017
A2BRANCH 1T215003100Q3 2017
A2BRANCH 1T24200 Q4 2017
A1BRANCH 1T3 2400Q1 2018
A1BRANCH 1T3 25402Q2 2018
A1BRANCH 1T410008172Q3 2018
A2BRANCH 1T43200 Q4 2018
A1BRANCH 2T1  Q1 2017
A2BRANCH 2T215001200Q2 2017
A1BRANCH 2T31899500Q3 2017
A1BRANCH 2T4  

Q4 2017

 

 


How the data should look like (the ones in RED are post the solution in place):

 

HEADBRANCHBRANCH SECTIONAMOUNT 1AMOUNT 2QUARTER YEAR
A1BRANCH 1T1 2456Q1 2017
A2BRANCH 1T1 2456Q2 2017
A2BRANCH 1T215003100Q3 2017
A2BRANCH 1T242003100Q4 2017
A1BRANCH 1T342002400Q1 2018
A1BRANCH 1T3420025402Q2 2018
A1BRANCH 1T410008172Q3 2018
A2BRANCH 1T432008172Q4 2018
A1BRANCH 2T1  Q1 2017
A2BRANCH 2T215001200Q2 2017
A1BRANCH 2T31899500Q3 2017
A1BRANCH 2T41899500Q4 2017

 

I tried using Calculate(FirstNonBlank() as well but without success.

 

 

Thank You

6 REPLIES 6
Ashish_Mathur
Super User
Super User

Hi,

 

Here's one way to do it with DAX.  I created the following 3 calculated column formulas

 

Date = DATE(RIGHT(Data[QUARTER YEAR],4),IF(LEFT(Data[QUARTER YEAR],2)="Q1",3,IF(LEFT(Data[QUARTER YEAR],2)="Q2",6,IF(LEFT(Data[QUARTER YEAR],2)="Q3",9,12))),1)

 

Revised amount1 = if(ISBLANK([AMOUNT 1]),LOOKUPVALUE([AMOUNT 1],Data[Date],CALCULATE(MAX(Data[Date]),FILTER(Data,Data[Date]<EARLIER(Data[Date])&&Data[BRANCH]=EARLIER(Data[BRANCH])&&[AMOUNT 1]>0)),Data[BRANCH],Data[BRANCH]),[AMOUNT 1])

 

Revised amount2 = if(ISBLANK([AMOUNT 2]),LOOKUPVALUE([AMOUNT 2],Data[Date],CALCULATE(MAX(Data[Date]),FILTER(Data,Data[Date]<EARLIER(Data[Date])&&Data[BRANCH]=EARLIER(Data[BRANCH])&&[AMOUNT 2]>0)),Data[BRANCH],Data[BRANCH]),[AMOUNT 2])

 

Hope this helps.

 

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
v-danhe-msft
Microsoft Employee
Microsoft Employee

Hi @nirvana_moksh,

Based on my test, you could refer to below steps in query editor:

1.Copy [AMOUNT 1] and fill down the copy columns:

1.PNG

2.Create a custom column with below code:

if [AMOUNT 1]=null and [BRANCH SECTION]="T1" then null else [#"AMOUNT 1 - Copy"]

1.PNG

You could also download the pbix file to have a view.

 

Regards,

Daniel He

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

@v-danhe-msft - But this won't be optimal in regards to when I integrate this to the whole data set, as that includes 1k+ branches and multiple Branch Sections as well. Also, wont filling from previous value cause wrong filling of data, for instance if last row of Branch 1 has a value and then Branch 2 starts fresh it will inherit the value of Branch 1.

@nirvana_moksh

 

This is one way in Power Query.

Basically we have to do a "GroupBy" branch step first and then apply the 'FillDown" function

 

Please see attached file as well for the steps

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("hZDNCoMwEIRfRXL2sDvZ1Hi0XjwVLL2J7/8a3Y1o2/WnEDIKH8OXmabQcajD/dk9+qGyz5ddeiDppjFyBeImzLWiOEYNwwVm/5yINCKXGOMfXFA4Q+UL9a5xdV1ai2u+RpMQNuETVkyYSmfmBptwPhY2PDrhfTPcYifDwi3GS/HPwL7V3sa5bTXSfl9Py8dh3XZ+Aw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [HEAD = _t, BRANCH = _t, #"BRANCH SECTION" = _t, #"AMOUNT 1" = _t, #"AMOUNT 2" = _t, #"QUARTER YEAR" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"HEAD", type text}, {"BRANCH", type text}, {"BRANCH SECTION", type text}, {"AMOUNT 1", Int64.Type}, {"AMOUNT 2", Int64.Type}, {"QUARTER YEAR", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"BRANCH"}, {{"All_Rows", each Table.FillDown(_,{"AMOUNT 1","AMOUNT 2"}), type table}}),
    #"Expanded All_Rows" = Table.ExpandTableColumn(#"Grouped Rows", "All_Rows", {"HEAD", "BRANCH SECTION", "AMOUNT 1", "AMOUNT 2", "QUARTER YEAR"}, {"HEAD", "BRANCH SECTION", "AMOUNT 1", "AMOUNT 2", "QUARTER YEAR"})
in
    #"Expanded All_Rows"

 

 

Greg_Deckler
Community Champion
Community Champion

See my article on Mean Time Before Failure (MTBF) which uses EARLIER: http://community.powerbi.com/t5/Community-Blog/Mean-Time-Between-Failure-MTBF-and-Power-BI/ba-p/3395...



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Thanks @Greg_Deckler, going over your post now.

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

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