Forum Discussion
Direct Query First and Last Dates
- 8 years ago
Then we need to create another new measure to calculate the count of productId.
Count ProductId = CALCULATE ( COUNTROWS ( PowerBiTable1 ), FILTER ( ALL ( PowerBiTable1 ), PowerBiTable1[ProductId] = MAX ( PowerBiTable1[ProductId] ) ) )And the final measure will be:
Identifier = IF ( PowerBiTable1[Count ProductId] = 1, "Unique", IF ( MIN ( PowerBiTable1[TransactionDate] ) = PowerBiTable1[FirstTransactionDate], "First", IF ( MAX ( PowerBiTable1[TransactionDate] ) = PowerBiTable1[LastTransactionDate], "Last", BLANK () ) ) )Thanks,
Xi Jin.
Yes, it is not possible to use EARLIER, CALCULATE, LASTDATE and FILTER used in a calculated column. There exists a lot of limitations while using Direct Query as data source.
Please refer: DirectQuery Modelling limitations
Then to achieve your requirement, you should do some workarounds. Please refer to following steps:
1. Create two Measures to calculate the FirstTransactionDate and LastTransactionDate:
FirstTransactionDate =
CALCULATE (
MIN ( PowerBiTable1[TransactionDate] ),
FILTER (
ALL ( PowerBiTable1 ),
PowerBiTable1[ProductId] = MAX ( PowerBiTable1[ProductId] )
)
)LastTransactionDate =
CALCULATE (
MAX ( PowerBiTable1[TransactionDate] ),
FILTER (
ALL ( PowerBiTable1 ),
PowerBiTable1[ProductId] = MAX ( PowerBiTable1[ProductId] )
)
)2. Create a Measure to get the identifier:
Identifier =
IF (
MIN ( PowerBiTable1[TransactionDate] ) = PowerBiTable1[FirstTransactionDate],
"First",
IF (
MAX ( PowerBiTable1[TransactionDate] ) = PowerBiTable1[LastTransactionDate],
"Last",
BLANK ()
)
)
Thanks,
Xi Jin.
- Boycie928 years agoResolver I
Hi v-xjiin-msft
Thank you so much for getting back to me.
Your solution works perfectly. However I have encountered an issue, that I hope you can help me with.
I never realised that I was excluding products that has only been sold once.
Can we edit the measures to say if the product ID appears once then its “Unique” if it appears Multiple times the first transaction (based on date) should say “first” and the last transaction (based on date) should say “Last”
Thanks in advance,
Boycie92
- v-xjiin-msft8 years agoSolution Sage
Then we need to create another new measure to calculate the count of productId.
Count ProductId = CALCULATE ( COUNTROWS ( PowerBiTable1 ), FILTER ( ALL ( PowerBiTable1 ), PowerBiTable1[ProductId] = MAX ( PowerBiTable1[ProductId] ) ) )And the final measure will be:
Identifier = IF ( PowerBiTable1[Count ProductId] = 1, "Unique", IF ( MIN ( PowerBiTable1[TransactionDate] ) = PowerBiTable1[FirstTransactionDate], "First", IF ( MAX ( PowerBiTable1[TransactionDate] ) = PowerBiTable1[LastTransactionDate], "Last", BLANK () ) ) )Thanks,
Xi Jin.- Boycie928 years agoResolver I
Hi v-xjiin-msft
Thanks for the help.
I have having an issue with the Identifier measure.
MIN ( PowerBiTable1[TransactionDate] ) = PowerBiTable1[FirstTransactionDate],
"First",
IF (
MAX ( PowerBiTable1[TransactionDate] ) = PowerBiTable1[LastTransactionDate],I get a red line at the bold parts of you formula. I looked at the error message and it says “a single value for column "" cannot be determined”
Any ideas why?
Thanks,
Boycie92