Join us for an expert-led overview of the tools and concepts you'll need to pass exam PL-300. The first session starts on June 11th. See you there!
Get registeredPower BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.
Hi, i have created a calculated column in my calendar table that shows the working day of the month, leaving the weekend days as blank (code below).
I cant seem to work out how to replicate this in Query editor however, any ideas?
WorkDayNumber =
VAR CurrentMonth = 'Calendar'[MonthYear]
VAR MonthTable = FILTER(ALL('Calendar'),'Calendar'[WeekdayWeekend]="Weekday" && 'Calendar'[MonthYear]= CurrentMonth)
Return
IF('Calendar'[WeekdayWeekend]="Weekend",
BLANK(),
RANKX(MonthTable, CALCULATE(AVERAGE('Calendar'[DayOfMonth])),,ASC))
Solved! Go to Solution.
See this code @adam_mac . Generally this wouldn't be super performant, but date tables rarely have more than a few thousand records and this works just fine for that purpose.
let
Source = {Number.From(#date(2021,1,1))..Number.From(#date(2021,12,31))},
#"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), {"Date"}, null, ExtraValues.Error),
#"Changed Type" = Table.TransformColumnTypes(#"Converted to Table",{{"Date", type date}}),
#"Inserted Year" = Table.AddColumn(#"Changed Type", "Year", each Date.Year([Date]), Int64.Type),
#"Inserted Month" = Table.AddColumn(#"Inserted Year", "Month", each Date.Month([Date]), Int64.Type),
#"Inserted Day" = Table.AddColumn(#"Inserted Month", "Day", each Date.Day([Date]), Int64.Type),
#"Added IsWorkday" = Table.AddColumn(#"Inserted Day", "IsWorkDay", each Date.DayOfWeek([Date],Day.Monday) < 5, Logical.Type),
#"Grouped Rows" =
Table.Group(
#"Added IsWorkday",
{"Year", "Month", "IsWorkDay"},
{
{
"All Rows",
each
let
varIsWorkDay = [IsWorkDay]{0}
in
if varIsWorkDay = true then Table.AddIndexColumn(_,"Workday of Month", 1, 1)
else Table.AddColumn(_, "Workday of Month", each null),
type table [Date=nullable date, Year=Int64.Type, Month=Int64,Type, Day=Int64.Type, IsWorkDay=logical, Workday of Month = Int64.Type]
}
}
),
#"Expanded All Rows" = Table.ExpandTableColumn(#"Grouped Rows", "All Rows", {"Date", "Day", "Workday of Month"}, {"Date", "Day", "Workday of Month"}),
#"Sorted Rows" = Table.Sort(#"Expanded All Rows",{{"Date", Order.Ascending}}),
#"Reordered Columns" = Table.ReorderColumns(#"Sorted Rows",{"Date", "Year", "Month", "IsWorkDay", "Workday of Month"})
in
#"Reordered Columns"
This is what it returns for 2021:
Note that I sorted and reordered the columns. That is 100% unnecessary for the result to load to the data model as sorting is irrelevant and column order is shown alphabetically. I just did it for the screenshot so you could see the weekends being ignored.
How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.
DAX is for Analysis. Power Query is for Data Modeling
Proud to be a Super User!
MCSA: BI ReportingSee this code @adam_mac . Generally this wouldn't be super performant, but date tables rarely have more than a few thousand records and this works just fine for that purpose.
let
Source = {Number.From(#date(2021,1,1))..Number.From(#date(2021,12,31))},
#"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), {"Date"}, null, ExtraValues.Error),
#"Changed Type" = Table.TransformColumnTypes(#"Converted to Table",{{"Date", type date}}),
#"Inserted Year" = Table.AddColumn(#"Changed Type", "Year", each Date.Year([Date]), Int64.Type),
#"Inserted Month" = Table.AddColumn(#"Inserted Year", "Month", each Date.Month([Date]), Int64.Type),
#"Inserted Day" = Table.AddColumn(#"Inserted Month", "Day", each Date.Day([Date]), Int64.Type),
#"Added IsWorkday" = Table.AddColumn(#"Inserted Day", "IsWorkDay", each Date.DayOfWeek([Date],Day.Monday) < 5, Logical.Type),
#"Grouped Rows" =
Table.Group(
#"Added IsWorkday",
{"Year", "Month", "IsWorkDay"},
{
{
"All Rows",
each
let
varIsWorkDay = [IsWorkDay]{0}
in
if varIsWorkDay = true then Table.AddIndexColumn(_,"Workday of Month", 1, 1)
else Table.AddColumn(_, "Workday of Month", each null),
type table [Date=nullable date, Year=Int64.Type, Month=Int64,Type, Day=Int64.Type, IsWorkDay=logical, Workday of Month = Int64.Type]
}
}
),
#"Expanded All Rows" = Table.ExpandTableColumn(#"Grouped Rows", "All Rows", {"Date", "Day", "Workday of Month"}, {"Date", "Day", "Workday of Month"}),
#"Sorted Rows" = Table.Sort(#"Expanded All Rows",{{"Date", Order.Ascending}}),
#"Reordered Columns" = Table.ReorderColumns(#"Sorted Rows",{"Date", "Year", "Month", "IsWorkDay", "Workday of Month"})
in
#"Reordered Columns"
This is what it returns for 2021:
Note that I sorted and reordered the columns. That is 100% unnecessary for the result to load to the data model as sorting is irrelevant and column order is shown alphabetically. I just did it for the screenshot so you could see the weekends being ignored.
How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.
DAX is for Analysis. Power Query is for Data Modeling
Proud to be a Super User!
MCSA: BI ReportingGlad it was useful for you @adam_mac
DAX is for Analysis. Power Query is for Data Modeling
Proud to be a Super User!
MCSA: BI ReportingCopy Calendar table,
Filter “Weekend” in the WeekWeekend column
Groupby MonthYear column, aggretating all data
Add column Table.AddIndexColumn([data], "Workday#", 1,1) ([data] being the aggregation of the grouping)
Expand and remove all columns but date and WorkdayNo
Left out join Calendar with copy
Et volia
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
Check out the June 2025 Power BI update to learn about new features.
User | Count |
---|---|
15 | |
10 | |
10 | |
8 | |
7 |