cancel
Showing results for
Did you mean:
Helper II

Calculated column for the sales of the month to create Custom Data Type

Hi,

I don't know if you know that Excel can display the contained of a Data Model table. The result is quiet interesting because you can use the Custom Data Type to associate your tables directly in a worksheet.

If you don't the principe, you specify in your data model a Table with a column ID and in Excel, you can call each one of the columns of that table like in this example

As you can see, I was able to create a calculated column for the last purchase date with this measure

LastDateAchat = MAX(Ventes[Date_Commande])

And I used the previous measure for the second calculated column to return the amount of the last purchase

ValueLastPurchase = CALCULATE([CA],FILTER(Ventes,Ventes[Date_Commande]=Clients[LastDatePurchase]))

But, I'm not able to find the calculation to return the amount of sales for the previous month

SalesLast_Month =
VAR Last_Month = DATE(YEAR(TODAY()),MONTH(TODAY())-1, 1)
RETURN IF (Clients[LastDatePurchase] >= Last_Month,
CALCULATE(
[CA],
FILTER(
Ventes,
Ventes[Date_Commande]>=DATE(YEAR(Clients[LastDatePurchase]), MONTH(Clients[LastDatePurchase])-1, 1))),
BLANK())

I know that wiht a measure, it's a piece of cake but with this calculation, the result isn't the one expected

And the data model is a basic one: Clients, Products and Sales (Ventes)

Any idea to return the sales for each Client ID with a calculated column?

Thanks

1 ACCEPTED SOLUTION
Helper II

Hi @selimovd ,

I have tried your solution but it doesn't solve the problem.

But I found a way to solve this with Power Query

I have created a temporary table with only the sales of the previous month and apply a group by.

And finally, I have merge the Client Table with this temparory tables.

It works, but I would prefer to do the job with a measure

2 REPLIES 2
Community Champion

Bonjour @FredLEGUEN ,

probably I found the mistake.

When you're working in a calculated column you should be aware of the context transition (when is value from the row and when from the whole table). The moment you use CALCULATE the context transition happens from the row to the filter context.

In your case this means you can't access the LastDatePurchase in the row anymore, so you have to save it before as a variable before the transition happens.

Try the following measure:

SalesLast_Month =
VAR Last_Month = DATE( YEAR( TODAY() ), MONTH( TODAY() ) - 1, 1 )
VAR vLastDatePurchase = Clients[LastDatePurchase]
RETURN
IF(
Clients[LastDatePurchase] >= Last_Month,
CALCULATE(
[CA],
FILTER(
Ventes,
Ventes[Date_Commande] >= DATE( YEAR( vLastDatePurchase ), MONTH( vLastDatePurchase ) - 1, 1 )
)
),
BLANK()
)

If you need any help please let me know.
If I answered your question I would be happy if you could mark my post as a solution ✔️ and give it a thumbs up 👍

Best regards
Denis

Helper II

Hi @selimovd ,

I have tried your solution but it doesn't solve the problem.

But I found a way to solve this with Power Query

I have created a temporary table with only the sales of the previous month and apply a group by.

And finally, I have merge the Client Table with this temparory tables.

It works, but I would prefer to do the job with a measure

Announcements