Forum Discussion

soft5's avatar
soft5
Frequent Visitor
3 years ago
Solved

Need help with DAX

Hello,  Please, please , someone help me with the following: I have data set ( sample below) Task is to count: 1. How many invoices were closed comparing to previous date ( don't appear next day)...
  • v-yueyunzh-msft's avatar
    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)+0
    Second 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])/2
    
    Third 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])/2
    
    Four 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