Forum Discussion
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
| item | transaction number | type | date | decs | branch |
| A | 01 | 3 | 01-01-2024 | purchase order | A |
| A | 01 | 22 | 10-01-2024 | Transfer order | A |
| A | 02 | 3 | 07-01-2024 | purchase order | A |
| A | 02 | 22 | 14-01-2024 | transfer | A |
| B | 03 | 3 | 01-01-2024 | purchase | A |
| B | 04 | 3 | 01-01-2024 | purchase | B |
| B | 03 | 22 | 13-01-2024 | transfer | B |
I want to Get the result like this
| item | transaction number | branch | Days Count |
| A | 01 | A | 9 |
| A | 02 | A | 7 |
| B | 04 | B | 12 |
21 Replies
- lbendlinSuper User
what happened to transaction number 03 ?
- Wadda7AboUdaiHelper I
still not transferd to branch
- lbendlinSuper User
so the last row should read 04?
BTW, where I come from Jan 1 to Jan 13 is 13 days, not 12 days.
- AnonymousNot 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.
- Wadda7AboUdaiHelper I
Hi Anonymous
thank you for your help
the table shows nothing , I think I did some thing wrongthe categoryreferance 3= purchse , 22 = transfer
- tamerj1Community Champion
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 )
)- Wadda7AboUdaiHelper 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 itemI want to show me the item 3 times each time how many day different
- tamerj1Community 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 ) ) )
- AnalyticsWizardSolution Supplier
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! 😊
- Wadda7AboUdaiHelper I
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
- AnonymousNot 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.