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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
gianmarco
Helper IV
Helper IV

[PowerQuery] Automatically create new data rows

Dear All,

I have the following table:

DateCost categoryBdg
01/01/2023Consultants5000
01/01/2023Software12000
01/01/2023Machinery5000
01/01/2023Insurance6000
01/01/2023Cleaning8000
01/01/2023Maintenance9000
01/01/2023Taxes10000

 

I want to automatically create the same exact rows till the end of the year.
In other words, create repetitions of column B and C, but column A should complete the months till 01/12/2023.

Is there any way to achieve this?

Thank you very much
gianmarco

2 REPLIES 2
Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Added Custom" = Table.ExpandListColumn(Table.AddColumn(Source, "Month number", each {1..12}),"Month number"),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom", each Date.AddMonths([Date],[Month number]-1), type date),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Date", "Month number"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Removed Columns",{{"Bdg", Int64.Type}, {"Custom", type date}})
in
    #"Changed Type"

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
lbendlin
Super User
Super User

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDUByIjAyNjJR0l5/y84tKcksS8kmIgz9TAwEApVgdNTXB+Wkl5YlEqkGlohFWFb2JyRmZealElTjM8gdYUJeYlgwwxw6rCOSc1MS8zLx3ItMBhSWZeSWoe1BBLrGpCEitSQR4xNABLxwIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, #"Cost category" = _t, Bdg = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Bdg", Currency.Type}}),
    Months = {1..12},
    #"Converted to Table" = Table.FromList(Months, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Added Custom" = Table.AddColumn(#"Converted to Table", "Custom", each Table.ReplaceValue(#"Changed Type",#date(2023, 1, 1),#date(2023, [Column1], 1),Replacer.ReplaceValue,{"Date"})),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Custom"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Other Columns", "Custom", {"Date", "Cost category", "Bdg"}, {"Date", "Cost category", "Bdg"})
in
    #"Expanded Custom"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done".

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.