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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Do57792
Advocate I
Advocate I

SQL / Power Query recursive - From Startdate to 12 month filled timespan

Hello experts !

 

I hope this is at the right part of the forum.

 

I have a table with project costs. The projects are clearly numbered. There is also a column with the start date and costs. (Very simplified). The costs are distributed over 12 months starting from the start date. In some cases the costs differ and are not total costs divided by 12.

I would like to have an overview table that shows the projects, the months and the amount for the month to make it easier in Power Bi and not have to map everything via Dax:


Origin data:

ProjectStartdateCosts
101 Jan 2025120
207 Sep 202012
309 Oct 202160



Target:

 

ProjectDatePartly Costs
1Jan 202510
1Feb 202510
1Mar 202510
1...10
1Dec 202510
2Sep 20201
2Oct20201
2...1
2Aug 20201
.........



The first idea was to map this using SQL and CTE.
Unfortunately I failed here. Is CTE supported at all? Within the interval, the plan was to use case queries to change the costs per property of the projects if necessary. I didn't get that far, because not even the body outputs the values accordingly.

 

 

WITH RECURSIVE months as (
    -- Basis: Startmonat (Monat 0)
    SELECT 

        Project,
        Costs,
        Startdate AS month_date,
        Costs/ 12.0 AS monthly_costs,
        0 AS month_number
    FROM Projects
    
    UNION ALL
    
    SELECT 
        Project,
        Costs,
        DATEADD(month,  1, Startdate) AS month_date,
        monthly_costs,
        month_number + 1
    FROM months
    WHERE month_number < 11 
)



SELECT 
    Project,
    month_date AS 'Month',
    monthly_costs AS 'Monthly Costs'
FROM months
ORDER BY Project, month_date

 

 




Is it necessary and also possible to map something like this via Power Query and if so, is there a corresponding guideline?

Many thanks in advance for any help.

1 ACCEPTED SOLUTION
Deku
Community Champion
Community Champion

On the CTE part, what SQL engine are you using? that is going to determine if recursion is supported.

 

If you want to use powerquery

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTIwVPBKzFMwMjAyBfIMjQyUYnWilYxAMuYKwakFIBkDsAxYwhgkYangn1wCkgAZYAbUEQsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Project = _t, Startdate = _t, Costs = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Project", Int64.Type}, {"Startdate", type date}, {"Costs", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "months", each 
List.Generate(() => 0, each _ < 12, each _ + 1)),
    #"Expanded 12 months" = Table.ExpandListColumn(#"Added Custom", "months"),
    #"Added Custom1" = Table.AddColumn(#"Expanded 12 months", "Dates", each Date.AddMonths([Startdate], [months])),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Partly Costs", each [Costs] / 12),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom2",{"Costs", "months"})
in
    #"Removed Columns"

 


Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!

View solution in original post

4 REPLIES 4
V-yubandi-msft
Community Support
Community Support

Hi @Do57792 ,

We noticed we haven't received a response from you yet, so we wanted to follow up and ensure the solution we provided addressed your issue. If you require any further assistance or have additional questions, please let us know.

Your feedback is valuable to us, and we look forward to hearing from you soon.

V-yubandi-msft
Community Support
Community Support

Hi @Do57792 ,

I wanted to check if you had the opportunity to review the information provided. Please feel free to contact us if you have any further questions. If my response has addressed your query, please accept it as a solution and give a 'Kudos' so other members can easily find it.

Thank you.

V-yubandi-msft
Community Support
Community Support

Hi @Do57792 ,

May I ask if you have resolved this issue? If so, please mark the helpful reply and accept it as the solution. This will be helpful for other community members who have similar problems to solve it faster.

Thank you.

Deku
Community Champion
Community Champion

On the CTE part, what SQL engine are you using? that is going to determine if recursion is supported.

 

If you want to use powerquery

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTIwVPBKzFMwMjAyBfIMjQyUYnWilYxAMuYKwakFIBkDsAxYwhgkYangn1wCkgAZYAbUEQsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Project = _t, Startdate = _t, Costs = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Project", Int64.Type}, {"Startdate", type date}, {"Costs", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "months", each 
List.Generate(() => 0, each _ < 12, each _ + 1)),
    #"Expanded 12 months" = Table.ExpandListColumn(#"Added Custom", "months"),
    #"Added Custom1" = Table.AddColumn(#"Expanded 12 months", "Dates", each Date.AddMonths([Startdate], [months])),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Partly Costs", each [Costs] / 12),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom2",{"Costs", "months"})
in
    #"Removed Columns"

 


Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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

Top Solution Authors