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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
ND_Pard
Helper II
Helper II

Power Query: How do I ensure every grouped record includes one of each item in a list

Our Fiscal Year ends on June 30th of each calendar year.
Each record of our database contains fields for the FYYY_FM, Department, and a Monetary_Amount.
I need to extract data for periods of 24 consecutive months from our database by Department.
Sometimes the data is historical and the Department may not have expenditures in that FYYY-FM and sometimes the data may include future FYYY_FM (i.e., months with zero amounts as they have not occurred yet).

How can I make sure my Power Query will always return a record for each Department for each of the desired 24 consecutive months even if they have no historical expenditures for that FYYY_FM or for the future FYYY_FM that I want to display?

I'm assuming I will need to use a "list", but am not sure how to accomplish this.

Here is some code that I currently use:

#"Filter the Subdivision" = Table.SelectRows(dbo_HISTORY, each Text.StartsWith([SUBDIVISION], "410-74")),
#"Filter only Expenses" = Table.SelectRows(#"Filter the Subdivision", each [ACCOUNT] >= "5"),
#"Filter FYYYY_FM" = Table.SelectRows(#"Filter only Expenses", each [FYYY_FM] >= "2022_01" and [FYYY_FM] <= "2023_12"),
#"Grouped Rows" = Table.Group(#"Filter FYYYY_FM", {"DEPARTMENT", "FYYY_FM"}, {{"Expenditures", each List.Sum([MONETARY_AMOUNT]), type nullable number}}),
#"Sort by DEPARTMENT & FYYYY_FM" = Table.Sort(#"Grouped Rows",{{"DEPARTMENT", Order.Ascending}, {"FYYY_FM", Order.Ascending}})

Thanks in advance; your suggestions are appreciated.

 

Currently get this:  Want this returned:
       
DEPARTMENTFYYY_FMExpenditures DEPARTMENTFYYY_FMExpenditures
99992022_02$ 15,275  99992022_01$ 0 
99992022_03$ 15,275  99992022_02$ 15,275 
99992022_05$ 30,550  99992022_03$ 15,275 
99992022_06$ 15,282  99992022_04$ 0 
99992022_07$ 15,280  99992022_05$ 30,550 
99992022_08$ 15,275  99992022_06$ 15,282 
99992022_09$ 15,275  99992022_07$ 15,280 
99992022_10$ 2  99992022_08$ 15,275 
99992022_11$ 30,550  99992022_09$ 15,275 
99992022_12$ 30,550  99992022_10$ 2 
99992023_02$ 15,313  99992022_11$ 30,550 
99992023_03$ 15,313  99992022_12$ 30,550 
99992023_04$ 15,313  99992023_01$ 0 
99992023_05$ 15,313  99992023_02$ 15,313 
99992023_06$ 15,313  99992023_03$ 15,313 
99992023_07$ 15,313  99992023_04$ 15,313 
99992023_08$ 15,313  99992023_05$ 15,313 
99992023_09$ 15,313  99992023_06$ 15,313 
    99992023_07$ 15,313 
    99992023_08$ 15,313 
    99992023_09$ 15,313 
    99992023_10$ 0 
    99992023_11$ 0 
    99992023_12$ 0 
1 ACCEPTED SOLUTION
v-stephen-msft
Community Support
Community Support

Hi @ND_Pard ,

 

You can create a complete yearmonth table and merge it into the main table.

1.Create a blank query in Power Query, hen copy and paste the following code into the advanced editor. You can also modify the specific date to change the start and end of the year.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtI10jVUio0FAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [StartDate = _t]),
    #"Added Custom" = Table.AddColumn(Source, "EndDate", each Date.FromText("2023-9-30")),
    #"Changed Type2" = Table.TransformColumnTypes(#"Added Custom",{{"EndDate", type date}}),
    #"Changed Type" = Table.TransformColumnTypes(#"Changed Type2",{{"StartDate", type date}}),
    #"Added Custom1" = Table.AddColumn(#"Changed Type", "Dates", each {Number.From([StartDate])..Number.From([EndDate])}),
    #"Expanded Dates" = Table.ExpandListColumn(#"Added Custom1", "Dates"),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Dates",{{"Dates", type date}}),
    #"Removed Columns1" = Table.RemoveColumns(#"Changed Type1",{"StartDate", "EndDate"}),
    #"Added Custom2" = Table.AddColumn(#"Removed Columns1", "Year", each Date.Year([Dates])),
    #"Changed Type3" = Table.TransformColumnTypes(#"Added Custom2",{{"Year", type text}}),
    #"Added Custom3" = Table.AddColumn(#"Changed Type3", "Month", each Text.PadStart(Text.From(Date.Month([Dates])),2,"0")),
    #"Changed Type4" = Table.TransformColumnTypes(#"Added Custom3",{{"Month", type text}}),
    #"Added Custom4" = Table.AddColumn(#"Changed Type4", "Custom", each [Year]&"_"&[Month]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom4",{"Dates", "Year", "Month"}),
    #"Removed Duplicates" = Table.Distinct(#"Removed Columns")
in
    #"Removed Duplicates"

 

vstephenmsft_0-1682479072885.png

2.Merge them. The matching column and the join kind are as follows.

33.png

vstephenmsft_6-1682479315972.png

 

3.Expand it and remove the original year-month column and rename the new one.

vstephenmsft_2-1682479252833.pngvstephenmsft_3-1682479261998.png

 

4.Replace null with 0.

vstephenmsft_4-1682479291924.png

vstephenmsft_5-1682479298447.png

You can also download my attachment for more details.

 

 

Best Regards,

Stephen Tao

 

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

View solution in original post

1 REPLY 1
v-stephen-msft
Community Support
Community Support

Hi @ND_Pard ,

 

You can create a complete yearmonth table and merge it into the main table.

1.Create a blank query in Power Query, hen copy and paste the following code into the advanced editor. You can also modify the specific date to change the start and end of the year.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtI10jVUio0FAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [StartDate = _t]),
    #"Added Custom" = Table.AddColumn(Source, "EndDate", each Date.FromText("2023-9-30")),
    #"Changed Type2" = Table.TransformColumnTypes(#"Added Custom",{{"EndDate", type date}}),
    #"Changed Type" = Table.TransformColumnTypes(#"Changed Type2",{{"StartDate", type date}}),
    #"Added Custom1" = Table.AddColumn(#"Changed Type", "Dates", each {Number.From([StartDate])..Number.From([EndDate])}),
    #"Expanded Dates" = Table.ExpandListColumn(#"Added Custom1", "Dates"),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Dates",{{"Dates", type date}}),
    #"Removed Columns1" = Table.RemoveColumns(#"Changed Type1",{"StartDate", "EndDate"}),
    #"Added Custom2" = Table.AddColumn(#"Removed Columns1", "Year", each Date.Year([Dates])),
    #"Changed Type3" = Table.TransformColumnTypes(#"Added Custom2",{{"Year", type text}}),
    #"Added Custom3" = Table.AddColumn(#"Changed Type3", "Month", each Text.PadStart(Text.From(Date.Month([Dates])),2,"0")),
    #"Changed Type4" = Table.TransformColumnTypes(#"Added Custom3",{{"Month", type text}}),
    #"Added Custom4" = Table.AddColumn(#"Changed Type4", "Custom", each [Year]&"_"&[Month]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom4",{"Dates", "Year", "Month"}),
    #"Removed Duplicates" = Table.Distinct(#"Removed Columns")
in
    #"Removed Duplicates"

 

vstephenmsft_0-1682479072885.png

2.Merge them. The matching column and the join kind are as follows.

33.png

vstephenmsft_6-1682479315972.png

 

3.Expand it and remove the original year-month column and rename the new one.

vstephenmsft_2-1682479252833.pngvstephenmsft_3-1682479261998.png

 

4.Replace null with 0.

vstephenmsft_4-1682479291924.png

vstephenmsft_5-1682479298447.png

You can also download my attachment for more details.

 

 

Best Regards,

Stephen Tao

 

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

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors