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

Get inspired! Check out the entries from the Power BI DataViz World Championships preliminary rounds and give kudos to your favorites. View the vizzies.

Reply
Do57792
Helper I
Helper 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 REPLY 1
Deku
Solution Supplier
Solution Supplier

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
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code FABINSIDER for a $400 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

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

March2025 Carousel

Fabric Community Update - March 2025

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

Top Kudoed Authors