Forum Discussion
Need help with DAX
- 3 years ago
Hi , soft5
Thanks for your quick response!
Here are the steps you can refer to :
(1)My test data is the same as yours.(2)We canc reate four measures to meet your need :
First Need = var _cur_date = MAX('Table'[Date]) var _pre_date = MAXX( FILTER( ALL('Table') , 'Table'[Date]<_cur_date) ,[Date]) var _cur_list = VALUES('Table'[INVOICE_ID]) var _pre_list = SELECTCOLUMNS( FILTER( ALL('Table') , 'Table'[Date] = _pre_date) , "invoices" , [INVOICE_ID]) var _list = EXCEPT(_pre_list , _cur_list) return COUNTROWS(_list)+0Second Need = var _cur_date = MAX('Table'[Date]) var _pre_date = MAXX( FILTER( ALL('Table') , 'Table'[Date]<_cur_date) ,[Date]) var _cur_list = VALUES('Table'[INVOICE_ID]) var _pre_list = SELECTCOLUMNS( FILTER( ALL('Table') , 'Table'[Date] = _pre_date) , "invoices" , [INVOICE_ID]) var _same_list = INTERSECT(_cur_list , _pre_list) var _t = FILTER( ALL('Table') , 'Table'[INVOICE_ID] in _same_list && OR( 'Table'[Date] = _cur_date , 'Table'[Date] =_pre_date) ) var _t2 = ADDCOLUMNS( _t , "flag" , var _cur_in =[INVOICE_ID] var _count = COUNTROWS(DISTINCT(SELECTCOLUMNS( FILTER(_t , [INVOICE_ID] = _cur_in),"status" , [Status]))) return IF(_count>1 ,1,0)) return SUMX(_t2, [flag])/2Third Need = var _cur_date = MAX('Table'[Date]) var _pre_date = MAXX( FILTER( ALL('Table') , 'Table'[Date]<_cur_date) ,[Date]) var _cur_list = VALUES('Table'[INVOICE_ID]) var _pre_list = SELECTCOLUMNS( FILTER( ALL('Table') , 'Table'[Date] = _pre_date) , "invoices" , [INVOICE_ID]) var _same_list = INTERSECT(_cur_list , _pre_list) var _t = FILTER( ALL('Table') , 'Table'[INVOICE_ID] in _same_list && OR( 'Table'[Date] = _cur_date , 'Table'[Date] =_pre_date) ) var _t2 = ADDCOLUMNS( _t , "flag" , var _cur_in =[INVOICE_ID] var _count = COUNTROWS(DISTINCT(SELECTCOLUMNS( FILTER(_t , [INVOICE_ID] = _cur_in),"status" , [Status]))) return IF(_count=1 ,1,0)) return SUMX(_t2, [flag])/2Four Need = var _cur_date = MAX('Table'[Date]) var _cur_list = VALUES('Table'[INVOICE_ID]) var _t = SELECTCOLUMNS( FILTER( ALL('Table') ,'Table'[Date]<_cur_date) , "invoices" , [INVOICE_ID]) var _t2 = EXCEPT(_cur_list , _t) return countrows(_t2)Then we can meet your need , the result is as follows:
Thank you for your time and sharing, and thank you for your support and understanding of PowerBI!
Best Regards,
Aniya Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly
Hello Aniya,
Thank you for responding and willingness to help
Claryfying your questions:
1. I need to count the invoices that have record on previous day and the following day those are not on the list. There should be distinct count of each record so for example for 4/11/2023 the resluft should be 3 -
| 22ac6 |
| 70050 |
| e0ad7 |
2 Yes it would be great to calculate it rolling, so shown for particular day - status change vs previous day
3. Ie ID 80856
| Company | INVOICE_ID | Status | Date |
| A | 80856 | open | 4/6/2023 |
| A | 80856 | open | 4/7/2023 |
| A | 80856 | failed | 4/11/2023 |
For 4/11/2023 this ID should count under status changed ( vs 4/7/2023)
For 4/7/2023 this ID should count under status unchanged ( vs 4/6/2023)
4. Distinct Count of IDs that haven't occured earlier than selected day. IE for 4/11/2023
the result is 3:
| 5eefd | 2 | ||
| bcaef | 1 | ||
| f7a73 | 1 |
I hope it helps.
Kind Regards
- v-yueyunzh-msft3 years agoCommunity Support
Hi , soft5
Thanks for your quick response!
Here are the steps you can refer to :
(1)My test data is the same as yours.(2)We canc reate four measures to meet your need :
First Need = var _cur_date = MAX('Table'[Date]) var _pre_date = MAXX( FILTER( ALL('Table') , 'Table'[Date]<_cur_date) ,[Date]) var _cur_list = VALUES('Table'[INVOICE_ID]) var _pre_list = SELECTCOLUMNS( FILTER( ALL('Table') , 'Table'[Date] = _pre_date) , "invoices" , [INVOICE_ID]) var _list = EXCEPT(_pre_list , _cur_list) return COUNTROWS(_list)+0Second Need = var _cur_date = MAX('Table'[Date]) var _pre_date = MAXX( FILTER( ALL('Table') , 'Table'[Date]<_cur_date) ,[Date]) var _cur_list = VALUES('Table'[INVOICE_ID]) var _pre_list = SELECTCOLUMNS( FILTER( ALL('Table') , 'Table'[Date] = _pre_date) , "invoices" , [INVOICE_ID]) var _same_list = INTERSECT(_cur_list , _pre_list) var _t = FILTER( ALL('Table') , 'Table'[INVOICE_ID] in _same_list && OR( 'Table'[Date] = _cur_date , 'Table'[Date] =_pre_date) ) var _t2 = ADDCOLUMNS( _t , "flag" , var _cur_in =[INVOICE_ID] var _count = COUNTROWS(DISTINCT(SELECTCOLUMNS( FILTER(_t , [INVOICE_ID] = _cur_in),"status" , [Status]))) return IF(_count>1 ,1,0)) return SUMX(_t2, [flag])/2Third Need = var _cur_date = MAX('Table'[Date]) var _pre_date = MAXX( FILTER( ALL('Table') , 'Table'[Date]<_cur_date) ,[Date]) var _cur_list = VALUES('Table'[INVOICE_ID]) var _pre_list = SELECTCOLUMNS( FILTER( ALL('Table') , 'Table'[Date] = _pre_date) , "invoices" , [INVOICE_ID]) var _same_list = INTERSECT(_cur_list , _pre_list) var _t = FILTER( ALL('Table') , 'Table'[INVOICE_ID] in _same_list && OR( 'Table'[Date] = _cur_date , 'Table'[Date] =_pre_date) ) var _t2 = ADDCOLUMNS( _t , "flag" , var _cur_in =[INVOICE_ID] var _count = COUNTROWS(DISTINCT(SELECTCOLUMNS( FILTER(_t , [INVOICE_ID] = _cur_in),"status" , [Status]))) return IF(_count=1 ,1,0)) return SUMX(_t2, [flag])/2Four Need = var _cur_date = MAX('Table'[Date]) var _cur_list = VALUES('Table'[INVOICE_ID]) var _t = SELECTCOLUMNS( FILTER( ALL('Table') ,'Table'[Date]<_cur_date) , "invoices" , [INVOICE_ID]) var _t2 = EXCEPT(_cur_list , _t) return countrows(_t2)Then we can meet your need , the result is as follows:
Thank you for your time and sharing, and thank you for your support and understanding of PowerBI!
Best Regards,
Aniya Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly
- soft53 years agoFrequent Visitor
Hello Aniya,
Thank you very much for sharing the query. It looks very promising. I like the logic of this solution. I still need to verify if it works for the full data set- and check if any adjustments are necessary.