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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
aTChris
Resolver I
Resolver I

Function to calculate per week

Hi everyone, I hope you are all well.

 

I have an amazing function created by @Mariusz . It calculates a monthly pro rated amount that I apply to a monthly revenue value. I then visualize that amount in a matrix.

I'm trying to update the function to report weekly and struggling. Can anyone help?

 

 

( startDate as date, endDate as date ) => let
    sYear = Date.Year( startDate ),
    sDate = #date( sYear, Date.Month( startDate ), 1 ),
    eYear = Date.Year( endDate ),
    eDate = Date.EndOfMonth( #date( eYear, Date.Month( endDate ), 1 ) ),
    listYears = List.Transform( { sYear..eYear }, each { _ } ),
    listToTable = #table( type table [ #"Years" = Int64.Type ], listYears ),
    addMonths = Table.ExpandListColumn( Table.AddColumn( listToTable, "Months", each { 1..12 } ) , "Months" ),
    getStartDate = Table.AddColumn( addMonths, "startDate", each #date( [Years], [Months], 1 ) )[ startDate ],
    selectStartDate = List.Select( getStartDate, each (_) >= sDate and (_) <= eDate ), 
    output = List.Transform( selectStartDate, 
        each 
        Record.FromList(
            {   
                ( Number.From( 
                    Duration.From( 
                        ( if Date.EndOfMonth( _ ) > endDate then endDate else Date.EndOfMonth( _ ) ) - 
                        ( if ( _ ) < startDate then startDate else ( _ ) ) 
                    ) 
                ) + 1 ) / ( Number.From( Duration.From( Date.EndOfMonth( _ ) - ( _ ) ) ) + 1 ) 
                , _ 
            }, type [pct = number, firstDate = date]  

        ) 
    )  
in  output

 

 

Thanks

 

Chris

1 ACCEPTED SOLUTION
Mariusz
Community Champion
Community Champion

Hi @aTChris 

 

Try this

( startDate as date, endDate as date ) => let
    dates = List.Dates( startDate, Duration.Days( endDate - startDate) + 1, #duration( 1, 0, 0, 0 ) ),
    transform = List.Transform( dates, each { _, ( Date.Year( _ ) * 100 ) + Date.WeekOfYear( _ ) } ),
    tableFrom = #table( type table [date= date, yearWeek = number ], transform ),
    group = Table.Group( tableFrom, {"yearWeek"}, {{"pct", each Table.RowCount(_) / 7, type number}}) 
in  group

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

Please feel free to connect with me.
LinkedIn

 

View solution in original post

2 REPLIES 2
Mariusz
Community Champion
Community Champion

Hi @aTChris 

 

Try this

( startDate as date, endDate as date ) => let
    dates = List.Dates( startDate, Duration.Days( endDate - startDate) + 1, #duration( 1, 0, 0, 0 ) ),
    transform = List.Transform( dates, each { _, ( Date.Year( _ ) * 100 ) + Date.WeekOfYear( _ ) } ),
    tableFrom = #table( type table [date= date, yearWeek = number ], transform ),
    group = Table.Group( tableFrom, {"yearWeek"}, {{"pct", each Table.RowCount(_) / 7, type number}}) 
in  group

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

Please feel free to connect with me.
LinkedIn

 

@Mariusz 

 

Perfect, thank you.

 

For anyone else I had to create a YearWeek col in my calendar table which wasn't as simple as id hoped. I added a custom col using the following to create the week number then merged with the year.

 

Text.PadStart(Text.From([Week of Year]),2,"0")

 

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors