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

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

Reply
williamy281
Frequent Visitor

Dynamic Matrix Table

I'm trying create a dynamic matrix table that will only show data with values.   Below, is how this would work, so if the data is ran on Monday, we want all data up to the Sunday (since Monday's data wouldn't be available). Then, on Tuesday, it would show all data up to the Monday., etc. so days would only show the last date of data.

williamy281_0-1598287603251.png

williamy281_1-1598287661381.png

 

 

1 ACCEPTED SOLUTION
edhans
Super User
Super User

Hi @williamy281 - I think I have it. This is about a 90% modeling issue, and 10% DAX and Matrix visual. This is the final result:

edhans_0-1598376155576.png

To look a this, here is the last week of data, which I had to fake. Your data stopped at Aug 13. I needed data through yesterday so the clock worked.

edhans_2-1598376265628.png

So today is Tuesday, Aug 25. The last row of the matrix shows a week starting on Tues the 18th and running through Monday the 24th. I had to do a few things.

First, I needed actual dates for your data. You can see those transformations in my PBIX file, but I needed to add the day number based on the row of your "report week" which has a repeated date, which is no good. I had to do some calculations to show the first week correctly if it isn't a full week (7 days) and the last week as well. Different calcs for each but you can see the M code in the file.

edhans_3-1598376469305.png

The code for just the day number is:

    #"Added Day Number" = 
        Table.AddColumn(
            #"Grouped Rows", 
            "Custom", 
            each 
                    let
                        varRowCount = Table.RowCount([AllRows]),
                        varCurrentDate = [Report Week]
                    in
                Table.AddIndexColumn(
                [AllRows], 
                "DayNumber",
                if varRowCount < 7
                then 
                    if List.Min(#"Grouped Rows"[Report Week]) = varCurrentDate
                    then 7-varRowCount
                    else 0
                else 0

                )
            )

Now, I had to have a good Date table that was equally dynamic. The Report Week gets moved to there. Always use a date table for dates. Full M code for the date table is:

let
    Source = 
        {
            Number.From(
            #date(
                Date.Year(
                    List.Min(Table[Date])
                    ),1,1
                )
            )..
            Number.From(
                #date(
                    Date.Year(
                        List.Max(Table[Date])
                        ),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),
    #"Inserted Quarter Number" = Table.AddColumn(#"Added Month Year", "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], Day.Thursday), 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),
    // Dynamically calculated each day
    #"Inserted Day of Week" = Table.AddColumn(#"Inserted Day", "Day of Week", each Date.DayOfWeek([Date], Date.DayOfWeek((DateTime.Date(DateTime.LocalNow())))), Int64.Type),
    #"Added Day of Week Sort" = Table.AddColumn(#"Inserted Day of Week", "Day of Week Sort", each [Day of Week] * -1, Int64.Type),
    #"Inserted Day of Year" = Table.AddColumn(#"Added Day of Week Sort", "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 First Day of Week" = Table.AddColumn(#"Added IsInCurrentYear", "First Day of Week", each Date.StartOfWeek([Date], Date.DayOfWeek((DateTime.Date(DateTime.LocalNow())))), type date),
    // Dynamically calculated every day
    #"Added Report Week" = 
        Table.AddColumn(
            #"Added First Day of Week", 
            "Report Week", 
            each 
                let
                    varFirstDayOfWeek = Date.DayOfWeek((DateTime.Date(DateTime.LocalNow())))
                in
            Text.From(Date.StartOfWeek([Date], varFirstDayOfWeek)) & " - " & Text.From(Date.AddDays(Date.StartOfWeek([Date], varFirstDayOfWeek), 6)),
            type text)
in
    #"Added Report Week"

Things to note on the date table:

  1. It is a full calendar year based on your data - note Source row gets its data from the min/max dates in your model then moves it to Jan 1 and Dec 31
  2. The Day of Week is dynamic based on today (refresh date) and is used to sort the weekday names - Date.DayOfWeek([Date], Date.DayOfWeek((DateTime.Date(DateTime.LocalNow()))))
  3. The Report Week (used in the matrix visual) is also dynamic, which you can see above - it is the last step.

Here is the PBIX.
This would have been much much easier had your source data had a real date in it, but I am assuming you are stuck with the export you are getting.

 

 



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

7 REPLIES 7
edhans
Super User
Super User

Hi @williamy281 - I think I have it. This is about a 90% modeling issue, and 10% DAX and Matrix visual. This is the final result:

edhans_0-1598376155576.png

To look a this, here is the last week of data, which I had to fake. Your data stopped at Aug 13. I needed data through yesterday so the clock worked.

edhans_2-1598376265628.png

So today is Tuesday, Aug 25. The last row of the matrix shows a week starting on Tues the 18th and running through Monday the 24th. I had to do a few things.

First, I needed actual dates for your data. You can see those transformations in my PBIX file, but I needed to add the day number based on the row of your "report week" which has a repeated date, which is no good. I had to do some calculations to show the first week correctly if it isn't a full week (7 days) and the last week as well. Different calcs for each but you can see the M code in the file.

edhans_3-1598376469305.png

The code for just the day number is:

    #"Added Day Number" = 
        Table.AddColumn(
            #"Grouped Rows", 
            "Custom", 
            each 
                    let
                        varRowCount = Table.RowCount([AllRows]),
                        varCurrentDate = [Report Week]
                    in
                Table.AddIndexColumn(
                [AllRows], 
                "DayNumber",
                if varRowCount < 7
                then 
                    if List.Min(#"Grouped Rows"[Report Week]) = varCurrentDate
                    then 7-varRowCount
                    else 0
                else 0

                )
            )

Now, I had to have a good Date table that was equally dynamic. The Report Week gets moved to there. Always use a date table for dates. Full M code for the date table is:

let
    Source = 
        {
            Number.From(
            #date(
                Date.Year(
                    List.Min(Table[Date])
                    ),1,1
                )
            )..
            Number.From(
                #date(
                    Date.Year(
                        List.Max(Table[Date])
                        ),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),
    #"Inserted Quarter Number" = Table.AddColumn(#"Added Month Year", "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], Day.Thursday), 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),
    // Dynamically calculated each day
    #"Inserted Day of Week" = Table.AddColumn(#"Inserted Day", "Day of Week", each Date.DayOfWeek([Date], Date.DayOfWeek((DateTime.Date(DateTime.LocalNow())))), Int64.Type),
    #"Added Day of Week Sort" = Table.AddColumn(#"Inserted Day of Week", "Day of Week Sort", each [Day of Week] * -1, Int64.Type),
    #"Inserted Day of Year" = Table.AddColumn(#"Added Day of Week Sort", "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 First Day of Week" = Table.AddColumn(#"Added IsInCurrentYear", "First Day of Week", each Date.StartOfWeek([Date], Date.DayOfWeek((DateTime.Date(DateTime.LocalNow())))), type date),
    // Dynamically calculated every day
    #"Added Report Week" = 
        Table.AddColumn(
            #"Added First Day of Week", 
            "Report Week", 
            each 
                let
                    varFirstDayOfWeek = Date.DayOfWeek((DateTime.Date(DateTime.LocalNow())))
                in
            Text.From(Date.StartOfWeek([Date], varFirstDayOfWeek)) & " - " & Text.From(Date.AddDays(Date.StartOfWeek([Date], varFirstDayOfWeek), 6)),
            type text)
in
    #"Added Report Week"

Things to note on the date table:

  1. It is a full calendar year based on your data - note Source row gets its data from the min/max dates in your model then moves it to Jan 1 and Dec 31
  2. The Day of Week is dynamic based on today (refresh date) and is used to sort the weekday names - Date.DayOfWeek([Date], Date.DayOfWeek((DateTime.Date(DateTime.LocalNow()))))
  3. The Report Week (used in the matrix visual) is also dynamic, which you can see above - it is the last step.

Here is the PBIX.
This would have been much much easier had your source data had a real date in it, but I am assuming you are stuck with the export you are getting.

 

 



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
Anonymous
Not applicable

HI @williamy281,

You can create a calculated table with all day of week value and setting up a default sort order, then you can add calculated column based on the last date from raw table and use 'day of week' value to calculate out the dynamic sort order and use as the sort order of new 'day of week' field.
After these steps, you can use 'day of week' to link raw table fields and use on the matrix to achieve dynamic sort order of columns field.

Custom Sorting in Power BI 

Regards,

Xiaoxin Sheng

amitchandak
Super User
Super User

@williamy281 , You have select this week data and take lastnonblankvaluel

you can use week rank for that

column in date table

Week Rank = RANKX(all('Date'),'Date'[Week Start date],,ASC,Dense)

measure
This Week = CALCULATE(lastnonblankvalue('Date'[date],sum('table'[Qty])), FILTER(ALL('Date'),'Date'[Week Rank]=max('Date'[Week Rank])))

 

for week refer

https://medium.com/@amitchandak.1978/power-bi-wtd-questions-time-intelligence-4-5-98c30fab69d3
https://community.powerbi.com/t5/Community-Blog/Week-Is-Not-So-Weak-WTD-Last-WTD-and-This-Week-vs-La...

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
edhans
Super User
Super User

Some test data would be helpful.

 

Much of this could be done using transformations in Power Query. For example, you need to sort your days of the week dynamically, where today should be last and 7 days ago would be first. The Date.DayOfWeek() function could be manipulated to also subtract today's Day of Week number to change the sort order. Then use the Sort By Columns feature to sort your days by this sort column. 

You can also use this column to limit what you bring into the model by filtering dynamically in Power Query.

 

Then the DAX and visual becomes easy.

But again, sample data with some actual expected output, not just "X" place holders, would really be needed to assist in building this logic.

 

How to get good help fast. Help us help you.
How to Get Your Question Answered Quickly
How to provide sample data in the Power BI Forum



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

I'm not sure if this is helpful, but the data currently looks like this:

There is not data for Wednesday because that's the day that I ran the data.

 

williamy281_0-1598291956714.png

williamy281_1-1598292083777.png

 

 

 

Hi @williamy281 - I'll post the link again. I am not typing that data in. Needs to be in a table format. Please see links below. Thanks!

 

How to get good help fast. Help us help you.
How to Get Your Question Answered Quickly
How to provide sample data in the Power BI Forum



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

Report WeekWeekDay of the Weektotal
7/2/2020 0:007/2/2020 - 7/8/2020   Monday6524
7/2/2020 0:007/2/2020 - 7/8/2020  Tuesday6540
7/2/2020 0:007/2/2020 - 7/8/2020Wednesday6529
7/9/2020 0:007/9/2020 - 7/15/2020 Thursday6032
7/9/2020 0:007/9/2020 - 7/15/2020   Friday5688
7/9/2020 0:007/9/2020 - 7/15/2020 Saturday1064
7/9/2020 0:007/9/2020 - 7/15/2020   Sunday1313
7/9/2020 0:007/9/2020 - 7/15/2020   Monday7468
7/9/2020 0:007/9/2020 - 7/15/2020  Tuesday6586
7/9/2020 0:007/9/2020 - 7/15/2020Wednesday5998
7/16/2020 0:007/16/2020 - 7/22/2020 Thursday6326
7/16/2020 0:007/16/2020 - 7/22/2020   Friday5512
7/16/2020 0:007/16/2020 - 7/22/2020 Saturday1057
7/16/2020 0:007/16/2020 - 7/22/2020   Sunday1098
7/16/2020 0:007/16/2020 - 7/22/2020   Monday7122
7/16/2020 0:007/16/2020 - 7/22/2020  Tuesday6317
7/16/2020 0:007/16/2020 - 7/22/2020Wednesday6608
7/23/2020 0:007/23/2020 - 7/29/2020 Thursday5969
7/23/2020 0:007/23/2020 - 7/29/2020   Friday5884
7/23/2020 0:007/23/2020 - 7/29/2020 Saturday1950
7/23/2020 0:007/23/2020 - 7/29/2020   Sunday2238
7/23/2020 0:007/23/2020 - 7/29/2020   Monday14409
7/23/2020 0:007/23/2020 - 7/29/2020  Tuesday13192
7/23/2020 0:007/23/2020 - 7/29/2020Wednesday12976
7/30/2020 0:007/30/2020 - 8/5/2020 Thursday12063
7/30/2020 0:007/30/2020 - 8/5/2020   Friday10304
7/30/2020 0:007/30/2020 - 8/5/2020 Saturday1004
7/30/2020 0:007/30/2020 - 8/5/2020   Sunday1105
7/30/2020 0:007/30/2020 - 8/5/2020   Monday7052
7/30/2020 0:007/30/2020 - 8/5/2020  Tuesday6202
7/30/2020 0:007/30/2020 - 8/5/2020Wednesday5834
8/6/2020 0:008/6/2020 - 8/12/2020 Thursday11660
8/6/2020 0:008/6/2020 - 8/12/2020   Friday11690
8/6/2020 0:008/6/2020 - 8/12/2020 Saturday2234
8/6/2020 0:008/6/2020 - 8/12/2020   Sunday2354
8/6/2020 0:008/6/2020 - 8/12/2020   Monday13555
8/6/2020 0:008/6/2020 - 8/12/2020  Tuesday10933
8/6/2020 0:008/6/2020 - 8/12/2020Wednesday6105
8/13/2020 0:008/13/2020 - 8/19/2020 Thursday6143
8/13/2020 0:008/13/2020 - 8/19/2020   Friday5632
8/13/2020 0:008/13/2020 - 8/19/2020 Saturday1106
8/13/2020 0:008/13/2020 - 8/19/2020   Sunday1082
8/13/2020 0:008/13/2020 - 8/19/2020   Monday7082
8/13/2020 0:008/13/2020 - 8/19/2020  Tuesday4750

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!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 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.