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

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

Reply
Anonymous
Not applicable

Script to create matrix table automatically

Hello, is it possible create a script to generate on matrix table every week automatically?

Per example I need that power bi generate theses table bellow when the data is updated. Every week with some filter the power bi create this report automacally per week. It is to compare in text week per week

2020-01-23              
NET Sales (       Gross Sales (in bags)      
               
TerritoryOpen
Orders
Invoiced
+ Rleased 
Sales
YTD
GAP
YTD
Total
Plan
%
Completed
 TerritoryOpen
Orders
Invoiced
+ Rleased 
Sales
YTD
GAP
YTD
Total
Plan
%
Completed
Centro Norte            10                15                25                 75             10025% Centro Norte            10                 15                25                 75             10025%
Oeste            10                15                25                 75             10025% Oeste            10                 15                25                 75             10025%
Sudeste            10                15                25                 75             10025% Sudeste            10                 15                25                 75             10025%
Sul            10                15                25                 75             10025% Sul            10                 15                25                 75             10025%
TOTAL            40                60             100               300             40025% TOTAL            40                 60             100               300             40025%
               
2020-01-30              
NET Sales (in bags)      Gross Sales (in bags)      
               
TerritoryOpen
Orders
Invoiced
+ Rleased 
Sales
YTD
GAP
YTD
Total
Plan
%
Completed
 TerritoryOpen
Orders
Invoiced
+ Rleased 
Sales
YTD
GAP
YTD
Total
Plan
%
Completed
Centro Norte            15                20                35                 65             10035% Centro Norte            15                 20                35                 65             10035%
Oeste            15                20                35                 65             10035% Oeste            15                 20                35                 65             10035%
Sudeste            15                20                35                 65             10035% Sudeste            15                 20                35                 65             10035%
Sul            15                20                35                 65             10035% Sul            15                 20                35                 65             10035%
TOTAL            60                80             140               260             40035% TOTAL            60                 80             140               260             40035%
               
2020-02-06              
NET Sales (in bags)      Gross Sales (in bags)      
               
TerritoryOpen
Orders
Invoiced
+ Rleased 
Sales
YTD
GAP
YTD
Total
Plan
%
Completed
 TerritoryOpen
Orders
Invoiced
+ Rleased 
Sales
YTD
GAP
YTD
Total
Plan
%
Completed
Centro Norte            10                15                25                 75             10025% Centro Norte            10                 15                25                 75             10025%
Oeste            10                15                25                 75             10025% Oeste            10                 15                25                 75             10025%
Sudeste            10                15                25                 75             10025% Sudeste            10                 15                25                 75             10025%
Sul            10                15                25                 75             10025% Sul            10                 15                25                 75             10025%
TOTAL            40                60             100               300             40025% TOTAL            40                 60             100               300             40025%
1 ACCEPTED SOLUTION
sturlaws
Resident Rockstar
Resident Rockstar

Hi @Anonymous 

 

there is no need for scripting or generating new matrices.

 

In your date/calendar table, create a new column like this:

yearWeekNum = year(dates[Date])*100+WEEKNUM(dates[Date])

 

Then create a second column like this:

relative week =
VAR _currentWeek =
    CALCULATE ( SELECTEDVALUE ( dates[yearWeekNum] ) )
VAR _todayWeek =
    YEAR ( TODAY () ) * 100
        + WEEKNUM ( TODAY () )
VAR _numberOfWeeks =
    IF (
        _currentWeek <= _todayWeek;
        CALCULATE (
            DISTINCTCOUNT ( dates[yearWeekNum] );
            FILTER (
                ALL ( dates );
                dates[yearWeekNum] >= _currentWeek
                    && dates[yearWeekNum] <= _todayWeek
            )
        ) - 1;
        CALCULATE (
            DISTINCTCOUNT ( dates[yearWeekNum] );
            FILTER (
                ALL ( dates );
                dates[yearWeekNum] <= _currentWeek
                    && dates[yearWeekNum] > _todayWeek
            )
        ) * -1
    )
RETURN
    _numberOfWeeks

 

Say you have 3 matrices, one for each of the 3 previous weeks. On the first one, add the column Dates[Relative week] to the filter section of the visual, and set it to filter for [Relative week]=1. For the next to matrices, set it to 2 and 3 respectively.

 

The code above sets the current week to 0, weeks in the past are positive numbers, weeks in the future is negative numbers.

 

Every time this model is updated, this column will be evaluated with respect to the current date, by the TODAY-function, so it will always filter the visuals according to the date when the model was last updated. 

Cheers,
Sturla

If this post helps, then please consider Accepting it as the solution. Kudos are nice too.

View solution in original post

1 REPLY 1
sturlaws
Resident Rockstar
Resident Rockstar

Hi @Anonymous 

 

there is no need for scripting or generating new matrices.

 

In your date/calendar table, create a new column like this:

yearWeekNum = year(dates[Date])*100+WEEKNUM(dates[Date])

 

Then create a second column like this:

relative week =
VAR _currentWeek =
    CALCULATE ( SELECTEDVALUE ( dates[yearWeekNum] ) )
VAR _todayWeek =
    YEAR ( TODAY () ) * 100
        + WEEKNUM ( TODAY () )
VAR _numberOfWeeks =
    IF (
        _currentWeek <= _todayWeek;
        CALCULATE (
            DISTINCTCOUNT ( dates[yearWeekNum] );
            FILTER (
                ALL ( dates );
                dates[yearWeekNum] >= _currentWeek
                    && dates[yearWeekNum] <= _todayWeek
            )
        ) - 1;
        CALCULATE (
            DISTINCTCOUNT ( dates[yearWeekNum] );
            FILTER (
                ALL ( dates );
                dates[yearWeekNum] <= _currentWeek
                    && dates[yearWeekNum] > _todayWeek
            )
        ) * -1
    )
RETURN
    _numberOfWeeks

 

Say you have 3 matrices, one for each of the 3 previous weeks. On the first one, add the column Dates[Relative week] to the filter section of the visual, and set it to filter for [Relative week]=1. For the next to matrices, set it to 2 and 3 respectively.

 

The code above sets the current week to 0, weeks in the past are positive numbers, weeks in the future is negative numbers.

 

Every time this model is updated, this column will be evaluated with respect to the current date, by the TODAY-function, so it will always filter the visuals according to the date when the model was last updated. 

Cheers,
Sturla

If this post helps, then please consider Accepting it as the solution. Kudos are nice too.

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.