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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
EZiamslow
Helper III
Helper III

Need help with Power Query for weekly and yearly based on my calculated columns

Hi,

I have a calendar table with two calculated columns for Fiscal weekly and quarterly. Is there a way to do that Power Query? The reason I want to do that in Power Query is I can't use (filter type = Top N) for calculated columns. I need to be able to filter using Top N for columns FYWW_C and FYQ_C on the page level. 

 

How would you make it work for (filter type = Top N) on page level?

 

Currently, Power BI hasn't supported. I already voted in Power BI ideas.

https://ideas.powerbi.com/forums/265200-power-bi-ideas/suggestions/13460142-page-and-report-level-fi...

 

 

 

FYWW_C = 
VAR __fw = Calendar[Week of Year] - 27 + 1
RETURN
    CONCATENATE (
        CONCATENATE (
                "FY",
                IF (
                    MONTH ( Calendar[Date] ) <= 6,
                    VALUE ( FORMAT ( Calendar[Date], "YY" ) ),
                    VALUE ( FORMAT ( Calendar[Date], "YY" ) ) + 1
                )
            ) & " ",
        CONCATENATE (
            "WW",
            IF ( __fw <= 0, FORMAT ( 52 + __fw, "00" ), FORMAT ( __fw, "00" ) )
        )
    )
FYQ_C = 
VAR __fw = Calendar[Week of Year] - 27 + 1
VAR quarter = Calendar[Quarter] - 3 + 1
RETURN
    IF (
        Calendar[Week of Year] = 1 && Calendar[Month] = 12 ||
        Calendar[Week of Year] = 27 && Calendar[Month] = 6 ||
        Calendar[Week of Year] = 40 && Calendar[Month] = 9,
        CONCATENATE (
            CONCATENATE ( "FY", VALUE ( FORMAT ( Calendar[Date], "YY" ) ) ),
            " Q" & quarter + 1
        ),
        CONCATENATE (
            CONCATENATE (
                "FY",
                IF (
                    MONTH ( Calendar[Date] ) <= 6,
                    VALUE ( FORMAT ( Calendar[Date], "YY" ) ),
                    VALUE ( FORMAT ( Calendar[Date], "YY" ) ) + 1
                )
            ) & " Q",
            IF ( quarter <= 0, 4 + quarter, quarter )
        )
    )

 

 

DateWeek of YearMonthQuarterFYWW_CFYQ_C
3/16/20201231FY20 WW38FY20 Q3
3/17/20201231FY20 WW38FY20 Q3
3/18/20201231FY20 WW38FY20 Q3
3/19/20201231FY20 WW38FY20 Q3
3/20/20201231FY20 WW38FY20 Q3
3/21/20201231FY20 WW38FY20 Q3
3/22/20201331FY20 WW39FY20 Q3
3/23/20201331FY20 WW39FY20 Q3
3/24/20201331FY20 WW39FY20 Q3
3/25/20201331FY20 WW39FY20 Q3
3/26/20201331FY20 WW39FY20 Q3
3/27/20201331FY20 WW39FY20 Q3
3/28/20201331FY20 WW39FY20 Q3
3/29/20201431FY20 WW40FY20 Q3
3/30/20201431FY20 WW40FY20 Q3
3/31/20201431FY20 WW40FY20 Q3
4/1/20201442FY20 WW40FY20 Q4
4/2/20201442FY20 WW40FY20 Q4
4/3/20201442FY20 WW40FY20 Q4
4/4/20201442FY20 WW40FY20 Q4
4/5/20201542FY20 WW41FY20 Q4
4/6/20201542FY20 WW41FY20 Q4
4/7/20201542FY20 WW41FY20 Q4
3 ACCEPTED SOLUTIONS
dax
Community Support
Community Support

Hi @EZiamslow , 

You could try below M code to see whether it work or not

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ldI5CoAwEIXhq0hqIbNl8QL2ViKS+1/DBRckKXzFDFP8X5Wsq1PP0QsJud6x7EuPY59xEermWfN9T+pKf4KEgoyCAQRCKGAUyAO0AYYaKAoMBQEFEQUJBRkF70tbDYwqoIQChoD5b3+MNHu7egF7BXsD+/dXhEbPVR/BPv3vywY=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Date = _t, #"Week of Year" = _t, Month = _t, Quarter = _t, FYWW_C = _t, FYQ_C = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Week of Year", Int64.Type}, {"Month", Int64.Type}, {"Quarter", Int64.Type}, {"FYWW_C", type text}, {"FYQ_C", type text}}),
    #"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"FYWW_C", "FYQ_C"}),
    #"Added Custom" = Table.AddColumn(#"Removed Columns", "Custom", each "FY"& (if Date.Month([Date])<= 6 then Number.ToText( Date.Year([Date])) else Number.ToText(Date.Year([Date])+1)) & " WW" & (if Date.WeekOfYear([Date])-27+1<=0 then Number.ToText(52+Date.WeekOfYear([Date])-27+1) else Number.ToText( Date.WeekOfYear([Date])-27+1))),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom.1", each "FY"& (if Date.Month([Date])<= 6 then Number.ToText( Date.Year([Date])) else Number.ToText(Date.Year([Date])+1)) & " Q" & (if Date.Month([Date])<= 6 then Number.ToText( Date.QuarterOfYear([Date])+2) else Number.ToText( Date.QuarterOfYear([Date])-2)))
in
    #"Added Custom1"

Best Regards,
Zoe Zhi

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

View solution in original post

because they are text valuies. There is no built in topn logic for text. You'd need to filter a numeric field, like the week number of year value in my date table above.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

View solution in original post

dax
Community Support
Community Support

Hi @EZiamslow , 

As I know, we could only use column in page level filter instead of measure or TopN. Top N is apply in visual level filter, so if you want to get Top N in viusal, you could add column in visual filter then filter it , Or you could create a measure which show Top N.

Best Regards,
Zoe Zhi

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

6 REPLIES 6
dax
Community Support
Community Support

Hi @EZiamslow , 

You could try below M code to see whether it work or not

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ldI5CoAwEIXhq0hqIbNl8QL2ViKS+1/DBRckKXzFDFP8X5Wsq1PP0QsJud6x7EuPY59xEermWfN9T+pKf4KEgoyCAQRCKGAUyAO0AYYaKAoMBQEFEQUJBRkF70tbDYwqoIQChoD5b3+MNHu7egF7BXsD+/dXhEbPVR/BPv3vywY=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Date = _t, #"Week of Year" = _t, Month = _t, Quarter = _t, FYWW_C = _t, FYQ_C = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Week of Year", Int64.Type}, {"Month", Int64.Type}, {"Quarter", Int64.Type}, {"FYWW_C", type text}, {"FYQ_C", type text}}),
    #"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"FYWW_C", "FYQ_C"}),
    #"Added Custom" = Table.AddColumn(#"Removed Columns", "Custom", each "FY"& (if Date.Month([Date])<= 6 then Number.ToText( Date.Year([Date])) else Number.ToText(Date.Year([Date])+1)) & " WW" & (if Date.WeekOfYear([Date])-27+1<=0 then Number.ToText(52+Date.WeekOfYear([Date])-27+1) else Number.ToText( Date.WeekOfYear([Date])-27+1))),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom.1", each "FY"& (if Date.Month([Date])<= 6 then Number.ToText( Date.Year([Date])) else Number.ToText(Date.Year([Date])+1)) & " Q" & (if Date.Month([Date])<= 6 then Number.ToText( Date.QuarterOfYear([Date])+2) else Number.ToText( Date.QuarterOfYear([Date])-2)))
in
    #"Added Custom1"

Best Regards,
Zoe Zhi

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

@dax 

 

Thanks! It works however, Power BI won't let me do (filter type = First N) on the page level.

because they are text valuies. There is no built in topn logic for text. You'd need to filter a numeric field, like the week number of year value in my date table above.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

@edhans 

It's possible for individual visual filter but not on page level.

dax
Community Support
Community Support

Hi @EZiamslow , 

As I know, we could only use column in page level filter instead of measure or TopN. Top N is apply in visual level filter, so if you want to get Top N in viusal, you could add column in visual filter then filter it , Or you could create a measure which show Top N.

Best Regards,
Zoe Zhi

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

edhans
Super User
Super User

Yes. This is what Power Query is for.

 

Paste this code into a blank query to see how this works. This is kinda long, but it is my standard date table with your FFWW_C column. I didn't do the other one because I think following the syntax, you can do it.

 

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

 

You are really interested in the last few rows where the "Added FFWW_C" step is.

 

in Power Query's formula window it looks like this:

2020-04-09 20_35_36-Custom Column.png

The only weird thing I did is PQ doesn't do number formatting the same as DAX, and in this case, it iwas just easier to subtract 2000 from the Date.Year() function than get into all of that. I grant my function will break in the year 3000. 😁

 

 

let
    Source = {Number.From(#date(2020,1,1))..Number.From(#date(2020,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 Month Name" = Table.AddColumn(#"Inserted Month", "Month Name", each Date.MonthName([Date]), type text),
    #"Added Short Month Name" = Table.AddColumn(#"Inserted Month Name", "Short Month Name", each Text.Start([Month Name],3), type text),
    #"Added Month Year" = Table.AddColumn(#"Added Short Month Name", "Month Year", each [Short Month Name] & " " & Text.From([Year]), type text),
    #"Added Month Year Sort" = Table.AddColumn(#"Added Month Year", "Month Year Sort", each [Year]*100 + [Month], Int64.Type),
    #"Inserted Quarter Number" = Table.AddColumn(#"Added Month Year Sort", "Quarter Number", each Date.QuarterOfYear([Date]), Int64.Type),
    #"Inserted Quarter" = Table.AddColumn( #"Inserted Quarter Number","Quarter", each "Qtr " & Text.From([Quarter Number]), type text),
    #"Inserted Quarter Year Sort" = Table.AddColumn(#"Inserted Quarter", "Quarter Year Sort", each [Year] * 10 + [Quarter Number], Int64.Type),
    #"Inserted Quarter Year" = Table.AddColumn(#"Inserted Quarter Year Sort", "Quarter Year", each "Q" & Text.End([Quarter],1) & " " & Text.From([Year]), type text),
    #"Inserted Week of Year" = Table.AddColumn(#"Inserted Quarter Year", "Week of Year", each Date.WeekOfYear([Date]), Int64.Type),
    #"Inserted Week of Month" = Table.AddColumn(#"Inserted Week of Year", "Week of Month", each Date.WeekOfMonth([Date]), Int64.Type),
    #"Inserted Day" = Table.AddColumn(#"Inserted Week of Month", "Day", each Date.Day([Date]), Int64.Type),
    #"Inserted Day of Week" = Table.AddColumn(#"Inserted Day", "Day of Week", each Date.DayOfWeek([Date]), Int64.Type),
    #"Inserted Day of Year" = Table.AddColumn(#"Inserted Day of Week", "Day of Year", each Date.DayOfYear([Date]), Int64.Type),
    #"Inserted Day Name" = Table.AddColumn(#"Inserted Day of Year", "Day Name", each Date.DayOfWeekName([Date]), type text),
    #"Added IsFuture Boolean" = Table.AddColumn(#"Inserted Day Name", "IsFuture", each [Date] > DateTime.Date(DateTime.LocalNow()), type logical),
    #"Added IsInCurrentWeek" = Table.AddColumn(#"Added IsFuture Boolean", "IsInCurrentWeek", each Date.IsInCurrentWeek([Date]), type logical),
    #"Added IsInCurrentMonth" = Table.AddColumn(#"Added IsInCurrentWeek", "IsInCurrentMonth", each Date.IsInCurrentMonth([Date]), type logical),
    #"Added IsInCurrentQuarter" = Table.AddColumn(#"Added IsInCurrentMonth", "IsInCurrentQuarter", each Date.IsInCurrentQuarter([Date]), type logical),
    #"Added IsInCurrentYear" = Table.AddColumn(#"Added IsInCurrentQuarter","IsInCurrentYear", each Date.IsInCurrentYear([Date]), type logical),
    #"Added FFWW_C" = Table.AddColumn(#"Added IsInCurrentYear", "FYWW_C", each "FY" & Text.From(
    (if Date.Month([Date]) <= 6
    then Date.Year([Date])
    else Date.Year([Date]) + 1) - 2000
) & " WW" & Text.From
(   if Date.WeekOfYear([Date]) - 27 + 1 <=0
    then Date.WeekOfYear([Date]) - 27 + 1 + 52
    else Date.WeekOfYear([Date]) - 27 + 1 ))
in
    #"Added FFWW_C"

 See the link below for making this date table fully dynamic where it will increase in size in the future as new dates come into your data set.

 

If you need help with the 2nd column, post back.

Creating a Dynamic Date Table in Power Query



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

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

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 Kudoed Authors