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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
ogend
Helper I
Helper I

Insert blank/ 0 dollar record if there is no record for a given date

hello, 

 

I have a table that looks like this:

PeriodScenarioDepartment Revenue
17-DecActualdepartment1500
18-MarForecastdepartment1550
18-JunForecastdepartment1600
18-SepForecastdepartment1500
18-DecForecastdepartment1530
18-DecActualdepartment1535
19-MarForecastdepartment1580
19-JunForecastdepartment1600
19-SepForecastdepartment1620
19-DecForecastdepartment1590
19-DecActualdepartment1600
20-MarForecastdepartment1620
20-JunForecastdepartment1630
20-SepForecastdepartment1620
20-DecForecastdepartment1610
20-DecActualdepartment1600
17-DecActualdepartment2575
18-MarForecastdepartment2633
18-JunForecastdepartment2690
18-SepForecastdepartment2575
18-DecForecastdepartment2610
18-DecActualdepartment2615
19-MarForecastdepartment2667
19-JunForecastdepartment2690
19-SepForecastdepartment2713
19-DecForecastdepartment2679
19-DecActualdepartment2690
20-MarForecastdepartment2713
20-JunForecastdepartment2725
20-SepForecastdepartment2713
20-DecForecastdepartment2702
20-DecActualdepartment2700
19-DecActualdepartment 3345
20-MarForecastdepartment 3357
20-JunForecastdepartment 3362
20-SepForecastdepartment 3357
20-DecForecastdepartment 3351
20-DecActualdepartment 3350

 

Department 3 Doesn't have any records prior to 2020. 

Would it be possilbe to do the following in Power query:  for each department for each  year where the recod where scenario "Actual" doesn't exist -insert a row with revenue equal to 0.

 

I am looking for this output- inserted two rows with 0s for dep 3

 

PeriodScenarioDepartment Revenue
17-DecActualdepartment1500
18-MarForecastdepartment1550
18-JunForecastdepartment1600
18-SepForecastdepartment1500
18-DecForecastdepartment1530
18-DecActualdepartment1535
19-MarForecastdepartment1580
19-JunForecastdepartment1600
19-SepForecastdepartment1620
19-DecForecastdepartment1590
19-DecActualdepartment1600
20-MarForecastdepartment1620
20-JunForecastdepartment1630
20-SepForecastdepartment1620
20-DecForecastdepartment1610
20-DecActualdepartment1600
17-DecActualdepartment2575
18-MarForecastdepartment2633
18-JunForecastdepartment2690
18-SepForecastdepartment2575
18-DecForecastdepartment2610
18-DecActualdepartment2615
19-MarForecastdepartment2667
19-JunForecastdepartment2690
19-SepForecastdepartment2713
19-DecForecastdepartment2679
19-DecActualdepartment2690
20-MarForecastdepartment2713
20-JunForecastdepartment2725
20-SepForecastdepartment2713
20-DecForecastdepartment2702
20-DecActualdepartment2700
17-DecActualdepartment 30
18-DecActualdepartment 30
19-DecActualdepartment 3345
20-MarForecastdepartment 3357
20-JunForecastdepartment 3362
20-SepForecastdepartment 3357
20-DecForecastdepartment 3351
20-DecActualdepartment 3350

 

Thank you!

 

1 ACCEPTED SOLUTION
Jimmy801
Community Champion
Community Champion

Hello @ogend 

 

first of all in your period-column the year is missing. And there are also missing some specification like which period to assign to the new row etc. What I tried to do is to add a new column that extracts the year. Then I grouped by Year and Deparment adding a function that takes the whole table. Then I added a new column where I checked if I can find the word "Actual" in the scenario-column. If no, I added a row, using the data from the grouped table. This means that you need to have a record in that year but not containing "Actual". If so, I added it using the first period found of this year. Hope you can understand my approach. Here the code of my solution

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMrTUMzDUMzIwtFTSUXLLL0pNTiwuATJTUgsSi0pyU/NKDIE8U1MDpVgdhGojA6CgY3JJaWIOploDLGopNtmIJJONYCbHAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Period = _t, Scenario = _t, Department = _t, Revenue = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Period", type date}, {"Scenario", type text}, {"Department", type text}, {"Revenue", Int64.Type}}, "de-DE"),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Year", each Date.Year([Period]), type number),
    #"Grouped Rows" = Table.Group(#"Added Custom", {"Department", "Year"}, {{"AllRows", each _, type table [Period=date, Scenario=text, Department=text, Revenue=number, Year=number]}}),
    #"Added Custom1" = Table.AddColumn(#"Grouped Rows", "HasActual", each if List.Contains([AllRows][Scenario],"Actual") then [AllRows] else Table.Combine({[AllRows], #table({"Department", "Year", "Period", "Scenario", "Revenue"},{{[Department], [Year], [AllRows][Period]{0}, "Actual", 0}})})),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"AllRows"}),
    #"Expanded HasActual" = Table.ExpandTableColumn(#"Removed Columns", "HasActual", {"Period", "Scenario", "Revenue"}, {"Period", "Scenario", "Revenue"})
in
    #"Expanded HasActual"

this transforms this

Jimmy801_0-1614417623958.png

 

into this

Jimmy801_1-1614417644469.png

 

adding in 2019 for dept1 a new row with 0 for actual using the 19.01.19 as period.

 

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

View solution in original post

6 REPLIES 6
Jimmy801
Community Champion
Community Champion

Hello @ogend 

 

I saw that you canceled your reply. So where you able to tweak the code?

 

BR

 

Jimmy

Hi @Jimmy801 , 

 

Thanks a lot for your guidance, it was superhelpful!

 

 

ogend
Helper I
Helper I

Hello @Jimmy801 , 


Thanks a lot fot your help!

I attempted to tweak your code to make it work with my Excel source file, but unfortunately it does not populate 0 line rows

would you be able to to take a look?

 

 

 

 

let
    Source = Excel.Workbook(File.Contents("C:\Users\Olga\Sample.xlsx"), null, true),
    Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],

    #"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Period", type date}, {"Scenario", type text}, {"Department", type text}, {"Revenue", Int64.Type}}),
//    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Period", type date}, {"Scenario", type text}, {"Department", type text}, {"Revenue", Int64.Type}}, "de-DE"),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Year", each Date.Year([Period]), type number),
    #"Grouped Rows" = Table.Group(#"Added Custom", {"Department", "Year"}, {{"AllRows", each _, type table [Period=date, Scenario=text, Department=text, Revenue=number, Year=number]}}),
    #"Added Custom1" = Table.AddColumn(#"Grouped Rows", "HasActual", each if List.Contains([AllRows][Scenario],"Actual") then [AllRows] else Table.Combine({[AllRows], #table({"Department", "Year", "Period", "Scenario", "Revenue"},{{[Department], [Year], [AllRows][Period]{0}, "Actual", 0}})})),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"AllRows"}),
    #"Expanded HasActual" = Table.ExpandTableColumn(#"Removed Columns", "HasActual", {"Period", "Scenario", "Revenue"}, {"Period", "Scenario", "Revenue"})
in
    #"Expanded HasActual"
//    #"Changed Type"

 

 

 

 

Jimmy801
Community Champion
Community Champion

Hello @ogend 

 

I cannot see any problems in the code. Are there some error messages? What is your final step showing? (Empty table or error). Could you make me please a screenshot of the data after the "Changed Type"-step. Did you check that your date-column is containing also a year? Could you share in case your sampel.xlsx?

 

BR

 

Jimmy

Jimmy801
Community Champion
Community Champion

Hello @ogend 

 

first of all in your period-column the year is missing. And there are also missing some specification like which period to assign to the new row etc. What I tried to do is to add a new column that extracts the year. Then I grouped by Year and Deparment adding a function that takes the whole table. Then I added a new column where I checked if I can find the word "Actual" in the scenario-column. If no, I added a row, using the data from the grouped table. This means that you need to have a record in that year but not containing "Actual". If so, I added it using the first period found of this year. Hope you can understand my approach. Here the code of my solution

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMrTUMzDUMzIwtFTSUXLLL0pNTiwuATJTUgsSi0pyU/NKDIE8U1MDpVgdhGojA6CgY3JJaWIOploDLGopNtmIJJONYCbHAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Period = _t, Scenario = _t, Department = _t, Revenue = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Period", type date}, {"Scenario", type text}, {"Department", type text}, {"Revenue", Int64.Type}}, "de-DE"),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Year", each Date.Year([Period]), type number),
    #"Grouped Rows" = Table.Group(#"Added Custom", {"Department", "Year"}, {{"AllRows", each _, type table [Period=date, Scenario=text, Department=text, Revenue=number, Year=number]}}),
    #"Added Custom1" = Table.AddColumn(#"Grouped Rows", "HasActual", each if List.Contains([AllRows][Scenario],"Actual") then [AllRows] else Table.Combine({[AllRows], #table({"Department", "Year", "Period", "Scenario", "Revenue"},{{[Department], [Year], [AllRows][Period]{0}, "Actual", 0}})})),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"AllRows"}),
    #"Expanded HasActual" = Table.ExpandTableColumn(#"Removed Columns", "HasActual", {"Period", "Scenario", "Revenue"}, {"Period", "Scenario", "Revenue"})
in
    #"Expanded HasActual"

this transforms this

Jimmy801_0-1614417623958.png

 

into this

Jimmy801_1-1614417644469.png

 

adding in 2019 for dept1 a new row with 0 for actual using the 19.01.19 as period.

 

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

PC2790
Community Champion
Community Champion

There may be better ways to do it but one the ones ways if:

If you have a continuous date table in Power Query having dates from the beinginning till the end, you can make a join of right outer join, the table above being on the left and date table on the right.

This will give you the missing dates in your table and null values.

You can replace null values according to your requirement

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

Top Solution Authors
Top Kudoed Authors