Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

Dynamic number of rows in a table

Hello,   I am trying to build a table in a PowerBI report where each row corresponds to a different date. If the current day is day 1st of the month, I want the table to show all the days of the pr...
  • jennratten's avatar
    3 years ago

    Hello - this will do it.  Please add a new blank query with the script below.  There are three different scenarios that you can use to test the result by commenting (adding two forward slashes) to varToday lines and removing them from one.

    Script

    let 
        // Test various scenarios
        //varToday = Date.From ( DateTime.FixedLocalNow() ),
        varToday = #date ( 2023, 8, 1 ),
        //varToday = #date ( 2023, 8, 2 ),
        Yesterday = Date.AddDays ( varToday, -1 ),
        CurrentMonthStart = Date.StartOfMonth ( varToday ),
        CurrentMonthEnd = Date.EndOfMonth ( varToday ),
        PriorMonthStart = Date.AddMonths ( CurrentMonthStart, -1 ),
        PriorMonthEnd = Date.EndOfMonth ( PriorMonthStart ),
        TodayIsCurrentMonthStart = varToday = CurrentMonthStart,
        minDate = if TodayIsCurrentMonthStart then PriorMonthStart else CurrentMonthStart,
        maxDate = if TodayIsCurrentMonthStart then PriorMonthEnd else Yesterday,
        ListDates = List.Dates(minDate, Number.From(maxDate) - Number.From(minDate) + 1,#duration(1,0,0,0)),
        ListToTable = Table.FromList(ListDates, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
        ChangeType = Table.TransformColumnTypes(ListToTable,{{"Column1", type date}}),
        RenameDate = Table.RenameColumns(ChangeType,{{"Column1", "Date"}})
    in
        RenameDate

     Result for 8/1/23

     

    Result for 8/21/2023

     

    Result for 8/2/2023