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.
Hello dear community first of all happy new year.
I have a purchase table like that:
Date | Client | Transaction Type | Invoice | Sales |
01/01/2022 10:05 | A | Redemption | A22 | -10 |
01/01/2022 10:00 | A | Purchase | A01 | 10 |
02/01/2022 15:00 | A | Purchase | A02 | 15 |
03/01/2022 11:00 | A | Purchase | A03 | 7 |
And my goal is to calculate the sum of sales after the first transaction (diferent invoince). In this case the result for customer A is 22.
For that I created three measures:
First Redemption =
var _maxdate = MAX(Date[Date] ) + 1
return
CALCULATE(
MIN(Sales[Date]),
FILTER(
ALL( 'Sales'),
Sales[Date] < _maxdate &&
'Sales'[Transaction Type] = "Redemption" )
)
Invoice =
var _date = [First Redemption]
var invoice =
CALCULATE(
MAX(Sales[Invoice] ),
KEEPFILTERS( 'Transacciones'[Date] = _data ) )
return invoice
Sales after fisrt redemption =
var _date = [First Redemption]
var invoice = [Invoice]
VAR __Sum =
CALCULATE(
[Sales],
FILTER(
all(Sales),
Sales[Date] > _data &&
Sales[Invoice] <> invoice),
VALUES('Sales'[Client]) )
RETURN if(isblank(_date), blank(), __Sum)
This is getting the right result, but it really takes a long time.
Any suggestions?
Thank you very much,
Simao
Solved! Go to Solution.
If your [First Purchase] measure is reasonable, I'd expect a variation of this to work OK but I don't have anything to test it against:
ADDCOLUMNS (
ADDCOLUMNS (
SUMMARIZE (
FILTER ( 'Sales', 'Sales'[Date] > DATE ( 2021, 11, 1 ) ),
'Sales'[Client]
),
"__firstRedemptionDate ", [First Purchase] + TIME ( 1, 0, 0 )
),
"Sales", CALCULATE ( [Sales], Sales[Date] > EARLIER ( [__firstRedemptionDate] ) )
)
@Anonymous Cool. Glad you have a solution in place. Cheers!!
Learn about conditional formatting at Microsoft Reactor
My latest blog post The Power of Using Calculation Groups with Inactive Relationships (Part 1) (perytus.com) I would ❤ Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!
⚡ Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.
Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!
Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo
If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤
Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.
@Anonymous glad it worked but just wondering what you mean by ending up using measure? I thought you were doing measures until now, no?
Learn about conditional formatting at Microsoft Reactor
My latest blog post The Power of Using Calculation Groups with Inactive Relationships (Part 1) (perytus.com) I would ❤ Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!
⚡ Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.
Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!
Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo
If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤
Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.
@parry2k For me it was indifferent whether it was a measure or a calculated table, I just wanted the most efficient way. @v-kkf-msft proposed a calculated table and I turned it into a measure
Hi @Anonymous ,
Please try this measure to see if it works.
Measure =
VAR FirstRedemption =
CALCULATE (
MIN ( Sales[Date] ),
FILTER (
ALL ( Sales ),
Sales[Client] = MAX(Sales[Client])
&& Sales[Transaction Type] = "Redemption"
)
)
RETURN
CALCULATE (
SUM ( Sales[Sales] ),
FILTER ( Sales, Sales[Date] > FirstRedemption )
)
If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.
Best Regards,
Winniz
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Thank you both, @parry2k and @AlexisOlson ,
But unfortunately it doesn't work yet. From what I've been researching the most efficient solution is to use ADDCOLUMNS - SUMMARIZE - FILTER, but I'm currently unable to pass the different first dates of purchase into the filter. The solution might be a measure or a table. It will look something like this:
(Table)
Sales after First Purchase =
VAR __firstRedemptionDate = [First Purchase] + TIME(1,0,0)
VAR _table =
addcolumns(
CALCULATETABLE(
SUMMARIZE(
FILTER(
'Sales',
'Sales'[Date] > date(2021,11,1)),
'Sales'[Client] ),
filter(
ALLEXCEPT('Sales', Sales[Date]),
Sales[Date] > __firstRedemptionDate)),
"Sales", [Sales])
return _table
But this way you also consider the first purchase.
Any other suggestion?
Once more thank you very much,
Simao
If your [First Purchase] measure is reasonable, I'd expect a variation of this to work OK but I don't have anything to test it against:
ADDCOLUMNS (
ADDCOLUMNS (
SUMMARIZE (
FILTER ( 'Sales', 'Sales'[Date] > DATE ( 2021, 11, 1 ) ),
'Sales'[Client]
),
"__firstRedemptionDate ", [First Purchase] + TIME ( 1, 0, 0 )
),
"Sales", CALCULATE ( [Sales], Sales[Date] > EARLIER ( [__firstRedemptionDate] ) )
)
Thank you very much!
I ended up adding a SUMX and turning it into a measure, but it worked fine!
Thank you also @parry2k for the help
@Anonymous try this measure
Sales after First Redemption Date =
VAR __firstRedemptionDate =
CALCULATE (
MIN ( 'Table'[Date ] ),
ALL ('Table' ),
VALUES ('Table'[Client ] ),
'Table'[Transaction Type ] = "Redemption"
)
RETURN
CALCULATE (
SUM ( 'Table'[Sales ] ),
'Table'[Date ] > __firstRedemptionDate,
'Table'[Transaction Type ] = "Purchase"
)
You can tweak it as you see fit, make sure to change table and column name as per your model.
Learn about conditional formatting at Microsoft Reactor
My latest blog post The Power of Using Calculation Groups with Inactive Relationships (Part 1) (perytus.com) I would ❤ Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!
⚡ Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.⚡
Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!
Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo
If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤
Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.
It appears that OP has a date table, so I'd encourage doing date filtering on that table instead. Having the time component in 'Table'[Date] makes this more complicated though.
@Anonymous, I'd encourage you to split your datetime column into a date column and a time column in your model for best performance. See this article for more detail:
https://radacad.com/how-to-use-time-and-date-dimensions-in-a-power-bi-model
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
109 | |
98 | |
77 | |
66 | |
54 |
User | Count |
---|---|
144 | |
104 | |
100 | |
86 | |
64 |