Tuesday, October 24, 2023

How to create and filter list of Dates using Power Query in Power BI

How to create and filter list of Dates using Power M-Query in Power BI
Scenario:
We want to create a List of Dates of Current Month and then Filter out the Dates of Week 2 and Week 4, using Power M-Query.
Next we want to use these Filtered List of Dates into another Date Table as a Filter based on the Parameter value.
We can achieve this Scenario, using the following following M-Query.
ListOfDates :

let
    vTodayDate = Date.From(DateTime.LocalNow()),
    vStartDate = Date.StartOfMonth(vTodayDate),
    vEndDate = Date.EndOfMonth(vTodayDate),
    
// Create List of Dates based on Current Month (Eg; October-2023) //
    vDatesList =     
    List.Dates(vStartDate, Duration.Days(vEndDate-vStartDate)+1, #duration(1,0,0,0)),
    
// Filter Week3,Week4 Dates //
    vFilterLsit = List.Select(vDatesList, each List.Contains ({2,4},Date.WeekOfMonth(_)))  
    
    /*
    vYear = Date.Year(vTodayDate),
    vQtr = Date.QuarterOfYear(vTodayDate),
    vMonth = Date.Month(vTodayDate),
    vMonthName = Date.MonthName(vTodayDate),
    vMonthWkNo = Date.WeekOfMonth(vTodayDate),
    vMonthWeek = "Week " & Number.ToText(vMonthWkNo)
    vPrevDay = Date.AddDays(vTodayDate,-1),
    vPrevWeek = Date.AddWeeks(vTodayDate,-1),
    // Start of Week: 0 for Sunday as Default; 1 for Monday //
    vPrevWkStartDt = Date.StartOfWeek(vPrevWeek)
    */

in
    vFilterLsit

Result:

-------------------------------------------------------------------------------------------------------------------
Next we we can use the above Filtered List of Dates (Week 3, Week 4) as Filter into another Date Table (DimCalendar), based on a Parameter Value.


Conditions:
If the Parameter "pDateFilterFlag" = 1 then Filter the Week 2 and Week 4 Dates.
If the Parameter "pDateFilterFlag" = 0 then Exclude the Week 2 and Week 4 Dates

DimCalendar:

let
/* Create the List of Dates based on Current Month */
vStartDate = Date.StartOfMonth(Date.From(DateTime.LocalNow())),
vEndDate = Date.EndOfMonth(Date.From(DateTime.LocalNow())),
DatesList = 
    List.Dates(vStartDate, Duration.Days(vEndDate-vStartDate)+1, #duration(1,0,0,0)),
DatesTable = 
    Table.FromList(DatesList, Splitter.SplitByNothing(), null, null, ExtraValues.Error),

DimDate = 
    Table.RenameColumns(DatesTable,{{"Column1", "DimDate"}}),
DimYear = 
    Table.AddColumn(DimDate, "DimYear", each Date.Year([DimDate])),
DimQtr = Table.AddColumn(DimYear, 
           "Qtr", each "Q" & Number.ToText(Date.QuarterOfYear([DimDate]))),
MonthNo = 
            Table.AddColumn(DimQtr, "MonthNo", each Date.Month([DimDate])),

// Filter Dates based on the List "ListOfDates" //
FilterWeekDates
            Table.SelectRows(MonthNo, each List.Contains (ListOfDates,[DimDate])),

/ Exclude Dates based on the List "ListOfDates" //
ExcludeWeekDates
            Table.SelectRows(MonthNo, each not List.Contains (ListOfDates,[DimDate])),

// Filter or exclude Week 2 and 4 Dates based on the Parameter (pDateFilterFlag) value. //
vResult = if pDateFilterFlag = 1 then FilterWeekDates else ExcludeWeekDates
in
    vResult

Result:

Thanks, TAMATAM

No comments:

Post a Comment

Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.