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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
alfredas81
Frequent Visitor

How to find MAT(Moving Annual Total) on ID/month level

Hello everybody,

I have one problem regarding agregation on annual basis by customer ID and month.I need to have annual sales amount yeach month and I use summarize function to get needed table

 

mat_monthly_cust_sales=SUMMARIZE(sales;sales[year_month];sales[user_id];"MAT";CALCULATE(SUM(sales[amount]);
DATESINPERIOD(sales[date]; LASTDATE(sales[date]); -1;YEAR
)
))

 

any help would be appreciated.

1 ACCEPTED SOLUTION

Thanks v-shex-msft,

actually I do not understand what does this part

"year_month", [date].[Year] * 100
+ [Value]

 

but I came to more elegant solution

ADDCOLUMNS(
CROSSJOIN(VALUES(sales[user_id]);VALUES('date table'[year_month]));
"MAT";
CALCULATE(
SUM(sales[amount]);
CALCULATETABLE(online;ALL('date table');DATESINPERIOD('date table'[Date];LASTDATE('date table'[Date]);-1;YEAR) )
)
)

View solution in original post

4 REPLIES 4
v-shex-msft
Community Support
Community Support

HI @alfredas81,

 

Please try to use below formula if it suitable for your requirement:

 

mat_monthly_cust_sales =
SUMMARIZE (
    sales;
    sales[year_month];
    sales[user_id];
    "MAT"; CALCULATE (
        SUM ( sales[amount] );
        FILTER (
            ALL ( sales );
            [date]
                >= DATE ( YEAR ( sales[date] ) - 1; MONTH ( sales[date] ); DAY ( sales[date] ) )
                && [date] <= MAX ( sales[date] )
        );
        VALUES ( sales[year_month] );
        VALUES ( sales[user_id] )
    )
)

 

If not help, please share some sample data to help test and coding formula.

 

Regards,

XIaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

Thanks v-shex-msft,

but your DAX does the same trick as mine, just it takes much more time to calculate because of values() filters(it calcucalates corectly the amount on curent month but does not take into account sales in previous months).

Basicaly my sales data is of shape:

 

user_id|date|calc_column(year_month)|amount

x|2017-01-11|201701|5

x|2017-02-21|201702|10

x|2018-03-07|201803|20

x|2018-04-11|201804|10

x|2018-05-21|201805|15

y|2017-01-11|201701|5

y|2017-02-21|201702|10

y|2018-03-07|201803|20

y|2018-04-11|201804|10

y|2018-05-21|201805|15

 

I would like to get table like this for user x(same for all the rest users in one table)

 

user_id|year_month|MAT

x|201701|5

x|201702|15

x|201703|15

x|201704|15

x|201705|15

x|201706|15

x|201707|15

x|201708|15

x|201709|15

x|201710|15

x|201711|15

x|201712|15

x|201803|20

x|201804|30

x|201805|45

 

 

Hi @alfredas81,

 

I modify dax formula based your sample data, please take a look at following formulas.

 

Table formula:

Expand Table = 
VAR _calendar =
    CROSSJOIN (
        VALUES ( '111'[user_id] ),
        SELECTCOLUMNS (
            CROSSJOIN ( VALUES ( '111'[date].[Year] ), GENERATESERIES ( 1, 12 ) ),
            "year_month", [date].[Year] * 100
                + [Value]
        )
    )
VAR original =
    SELECTCOLUMNS (
        '111',
        "user_id", [user_id],
        "year_month", [calc_column(year_month)],
        "amount", [amount]
    )
VAR remain =
    ADDCOLUMNS (
        EXCEPT (
            _calendar,
            SELECTCOLUMNS ( original, "user_id", [user_id], "year_month", [year_month] )
        ),
        "amount", 0
    )
RETURN
    UNION ( remain, original )

Calculate column rolling total:

rolling amount = 
CALCULATE (
    SUM ( 'Expand Table'[amount] ),
    FILTER (
        ALL ( 'Expand Table' ),
        [user_id] = EARLIER ( [user_id] )
            && [year_month] <= EARLIER ( [year_month] )
            && LEFT ( [year_month], 4 ) = LEFT ( EARLIER ( 'Expand Table'[year_month] ), 4 )
    )
)

Result:

3.PNG

 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

Thanks v-shex-msft,

actually I do not understand what does this part

"year_month", [date].[Year] * 100
+ [Value]

 

but I came to more elegant solution

ADDCOLUMNS(
CROSSJOIN(VALUES(sales[user_id]);VALUES('date table'[year_month]));
"MAT";
CALCULATE(
SUM(sales[amount]);
CALCULATETABLE(online;ALL('date table');DATESINPERIOD('date table'[Date];LASTDATE('date table'[Date]);-1;YEAR) )
)
)

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.