Forum Discussion

Wadda7AboUdai's avatar
2 years ago

calculate days count between 2 dates

Hello Everyone 

I want to calculate days starts with item purchese date to item transfer date , items can be more than ones in the table it means can be purchesd more than ones and for all branches , I want to calculate the days  for each branch and sorted by dates if there are more than one purches order 

 

Here is the source table 

 

itemtransaction numbertypedatedecsbranch
A01301-01-2024purchase orderA
A012210-01-2024Transfer orderA
A02307-01-2024purchase orderA
A022214-01-2024transferA
B03301-01-2024purchaseA
B04301-01-2024purchaseB
B032213-01-2024transferB

 

I want to Get the result like this 

itemtransaction numberbranchDays Count
A01A
A02A7
B04B12

 

 

21 Replies

      • lbendlin's avatar
        lbendlin
        Super User

        so the last row should read 04?

         

        BTW, where I come from Jan 1 to Jan 13 is 13 days, not 12 days.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Wadda7AboUdai ,

    lbendlin , thanks for your attention to this situation. I tried to modify the data myself and implement the result according to the user's request. Please check if it can be improved. Here is my solution:


    1. create a calculated column to get the number of days between intervals.

    Days Count =
    VAR PurchaseDate = CALCULATE(MIN('Table'[date]), 'Table'[decs] = "purchase", ALLEXCEPT('Table', 'Table'[item], 'Table'[branch],'Table'[transaction number]))
    VAR TransferDate = CALCULATE(MAX('Table'[date]), 'Table'[decs] = "transfer", ALLEXCEPT('Table', 'Table'[item], 'Table'[branch],'Table'[transaction number]))
    RETURN
    DATEDIFF(PurchaseDate, TransferDate, DAY)


    2. Create a calculation table to get the desired result.

    Summary Table =
    var _table=
    SUMMARIZE(
        'Table',
        'Table'[item],
        'Table'[transaction number],
        'Table'[branch],
        'Table'[Days Count]
    )
    return
    FILTER(
        _table,[Days Count]<>BLANK())

    If your Current Period does not refer to this, please clarify in a follow-up reply.

     

    Best Regards,

    Clara Gong

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

    • Wadda7AboUdai's avatar
      Wadda7AboUdai
      Helper I

      Hi Anonymous 
      thank you for your help 
      the table shows nothing , I think I did some thing wrong 

       

       

       

      the categoryreferance  3= purchse , 22 = transfer 

       

  • tamerj1's avatar
    tamerj1
    Community Champion

    Hi Wadda7AboUdai 

    this can be a measure. Place [Item], [TransactionNumber] & [Branch] in a table visual along with the following measure:

    Days Count =
    VAR CrrentTransactions = 'inventtransn'
    VAR PurchaseDate =
    CALCULATE (
    MIN ( ' inventtransn'[DATEPHYSICAL] ),
    'inventtransn'[REFERENCECATEGORY] = 3
    )
    VAR TransferDate =
    CALCULATE (
    MAX ( ' inventtransn'[DATEPHYSICAL] ),
    'inventtransn'[REFERENCECATEGORY] = 22
    )
    RETURN
    IF (
    NOT ISBLANK ( TransferDate ),
    DATEDIFF ( PurchaseDate, TransferDate, DAY )
    )

    • Wadda7AboUdai's avatar
      Wadda7AboUdai
      Helper I

      tamerj1 
      Thank you sp much for your help this worked fine with me when the item comes one time , when item comes more than one time I want to calculate days between REFERENCECATEGORY 3 and the next REFERENCECATEGORY 22 it means first find the first purchase date and go to next transdate not to last trans date , in the below picture it should calculate 3 time for the same item 

      I want to show me the item 3 times each time how many day different 

      • tamerj1's avatar
        tamerj1
        Community Champion

        Wadda7AboUdai 
        Yes that is because in expected result that you have presented, the transaction number is part of the filter context. If you place the transaction number in the result summary table it should work. However, the following should work in both senarios.

        Days Count = 
        SUMX ( 
            SUMMARIZE ( 
                'inventtransn',
                'inventtransn'[item],
                'inventtransn'[transaction number]
            ),
            VAR PurchaseDate =
                CALCULATE (
                    MIN ( 'inventtransn'[DATEPHYSICAL] ),
                    'inventtransn'[REFERENCECATEGORY] = 3
                )
            VAR TransferDate =
                CALCULATE (
                    MAX ( 'inventtransn'[DATEPHYSICAL] ),
                    'inventtransn'[REFERENCECATEGORY] = 22
                )
            RETURN
                IF (
                    NOT ISBLANK ( TransferDate ),
                    DATEDIFF ( PurchaseDate, TransferDate, DAY )
                )
        )
  • Wadda7AboUdai 

    To calculate the number of days between the purchase date and the transfer date for each item in Power BI, you can create a new column using the DATEDIFF function12. Here’s an example of how you might write this:

    Days Count = 
    CALCULATE(
        DATEDIFF(
            MINX(FILTER(Table1, Table1[type] = 3), Table1[date]), 
            MAXX(FILTER(Table1, Table1[type] = 22), Table1[date]), 
            DAY
        ),
        ALLEXCEPT(Table1, Table1[item], Table1[transaction number], Table1[branch])
    )

    In this formula, Table1 is the name of your table. This formula calculates the number of days between the minimum date (purchase date) and the maximum date (transfer date) for each combination of item, transaction number, and branch. The ALLEXCEPT function is used to remove any filters that might limit the rows being evaluated, except for the filters on the item, transaction number, and branch columns12.

    Please replace Table1 with your actual table name. If you need further assistance or have more specific requirements, feel free to ask! 😊

    • Wadda7AboUdai's avatar
      Wadda7AboUdai
      Helper I

      Hi AnalyticsWizard 

      Thank you for your Replay , I want to inform you that the transaction number is different between purchase and transfer order 

      the purchase order transaction number can be (scr-10023)

      the transfer number can by (scr-2324)

      and so on there is no related betwwen them 

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Wadda7AboUdai ,

     

    Could you please tell me if your problem has been solved?

     

    If it is, could you please mark the helpful replies as Answered to close this topic?

     

    Best Regards,

    Clara Gong

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.