Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
jayman
Regular Visitor

Calculating days in specific location for devices

I am trying to calculate the days a device spent in a specific location. I figured it out in excel while venting my data. But I cannot figure out how to translate it over to Power BI for my reports.

 

The excel formula is used in "Days in L46" is

[ =IF(C2=C3,IF(F2="L46",DAYS(E2,E3),"0"),"0") ].

 

This gives me the days between transactions into, while in, and out of the specific location.

The next step would be to get a sum of the days for each device. And the formula I used for this in "Total Days in L46" is

[ =SUMIF(B:B,B2,I:I) ]

 

TRANSACTION_IDSERIAL_NUMBERC_ATTRIBUTE1RCV_DATETRANSACTION_DATESUBINVENTORY_CODELOCATOR_CODETIFDays in L46Total Days in L46
36689307330310673314/7/202410-SEP-24L19L19WIPCC.Q3.025601
36689307320310673314/7/202410-SEP-24L17L17EXCPHLD.0.025601
35909471810310673314/7/202417-JUL-24L17L17EXCPHLD.0.025601
35909471800310673314/7/202417-JUL-24L02L02ST.0.025601
35397696130310673314/7/202410-JUN-24L02L02ST.0.025601
35397696120310673314/7/202410-JUN-24L19L19WIPCC.Q1.025601
34753792630310673314/7/202419-APR-24L19L19WIPCC.Q1.025601
34753792620310673314/7/202419-APR-24L46L46HOLD.1.1925611
34744321470310673314/7/202418-APR-24L46L46HOLD.1.1925601
34744321460310673314/7/202418-APR-24L46L46HOLD.1.1925601
34743891610310673314/7/202418-APR-24L46L46HOLD.1.1925601
34743891600310673314/7/202418-APR-24L15L15MGMT.0.025601
34602092300310673314/7/202407-APR-24L15L15MGMT.0.025601
34602092290310673314/7/202407-APR-24L15L15MGMT.0.025601
34602090950310673314/7/202407-APR-24L15L15MGMT.0.025601

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Thanks for the reply from danextian and Kedar_Pande, please allow me to provide another insight.
Hi @jayman ,

Add an index column.

vdengllimsft_0-1734943533363.png

 


Create the following calculated columns.

Days in L46 = 
VAR currentIndex = 'Table'[Index]
VAR nextC_ATTRIBUTE1 =
    MAXX (
        FILTER ( 'Table', 'Table'[Index] = currentIndex + 1 ),
        'Table'[C_ATTRIBUTE1]
    )
VAR nextTRANSATION_DATE =
    MAXX (
        FILTER ( 'Table', 'Table'[Index] = currentIndex + 1 ),
        'Table'[TRANSACTION_DATE]
    )
RETURN
    IF (
        nextC_ATTRIBUTE1 = 'Table'[C_ATTRIBUTE1],
        IF (
            'Table'[SUBINVENTORY_CODE] = "L46",
            DATEDIFF ( nextTRANSATION_DATE, 'Table'[TRANSACTION_DATE], DAY ),
            0
        ),
        0
    )
Total Days in L46 = 
SUMX (
    FILTER ( 'Table', 'Table'[SERIAL_NUMBER] = EARLIER ( 'Table'[SERIAL_NUMBER] ) ),
    'Table'[Days in L46]
)


The final result is as follows.

vdengllimsft_1-1734943613211.png

 

Please see the attahed pbix for reference.
 
Best Regards,
Dengliang Li

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

View solution in original post

6 REPLIES 6
Anonymous
Not applicable

Thanks for the reply from danextian and Kedar_Pande, please allow me to provide another insight.
Hi @jayman ,

Add an index column.

vdengllimsft_0-1734943533363.png

 


Create the following calculated columns.

Days in L46 = 
VAR currentIndex = 'Table'[Index]
VAR nextC_ATTRIBUTE1 =
    MAXX (
        FILTER ( 'Table', 'Table'[Index] = currentIndex + 1 ),
        'Table'[C_ATTRIBUTE1]
    )
VAR nextTRANSATION_DATE =
    MAXX (
        FILTER ( 'Table', 'Table'[Index] = currentIndex + 1 ),
        'Table'[TRANSACTION_DATE]
    )
RETURN
    IF (
        nextC_ATTRIBUTE1 = 'Table'[C_ATTRIBUTE1],
        IF (
            'Table'[SUBINVENTORY_CODE] = "L46",
            DATEDIFF ( nextTRANSATION_DATE, 'Table'[TRANSACTION_DATE], DAY ),
            0
        ),
        0
    )
Total Days in L46 = 
SUMX (
    FILTER ( 'Table', 'Table'[SERIAL_NUMBER] = EARLIER ( 'Table'[SERIAL_NUMBER] ) ),
    'Table'[Days in L46]
)


The final result is as follows.

vdengllimsft_1-1734943613211.png

 

Please see the attahed pbix for reference.
 
Best Regards,
Dengliang Li

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

Thank you for this solution. It worked perfectly for the sample date I had and the full data I had imported to my report.

Kedar_Pande
Super User
Super User

@jayman 

Create a calculated column:

Days in L46 = 
IF(
'Table'[SERIAL_NUMBER] = CALCULATE(
MAX('Table'[SERIAL_NUMBER]),
ALLEXCEPT('Table', 'Table'[TRANSACTION_ID])
) &&
'Table'[LOCATOR_CODE] = "L46",
DATEDIFF('Table'[TRANSACTION_DATE],
CALCULATE(
MIN('Table'[TRANSACTION_DATE]),
ALLEXCEPT('Table', 'Table'[SERIAL_NUMBER], 'Table'[LOCATOR_CODE])
),
DAY),
0
)

Create a calculated column:

Total Days in L46 = 
CALCULATE(
SUM('Table'[Days in L46]),
FILTER('Table', 'Table'[SERIAL_NUMBER] = EARLIER('Table'[SERIAL_NUMBER]))
)

💌 If this helped, a Kudos 👍 or Solution mark would be great! 🎉
Cheers,
Kedar
Connect on LinkedIn

danextian
Super User
Super User

Your sample data seems to be missing a column that indicates the grouping of the records -  company name, device id, etc - so the result of the calculated column below will not be compartmentalized.

Days = 
VAR PrevRow =
    FILTER (
        'Table',
        'Table'[TRANSACTION_DATE] < EARLIER ( 'Table'[TRANSACTION_DATE] )
            && 'Table'[SUBINVENTORY_CODE] = "L46"
    )
VAR PrevDate =
    MAXX ( PrevRow, [TRANSACTION_DATE] )
RETURN
    IF (
        'Table'[SUBINVENTORY_CODE] = "L46",
        DATEDIFF ( PrevDate, 'Table'[TRANSACTION_DATE], DAY )
    ) + 0

Total Days = 
SUM ( 'Table'[Days] )

Days2 = 
--if chronological order is based on transaction id
VAR PrevRow =
    FILTER (
        'Table',
        'Table'[TRANSACTION_ID] < EARLIER ( 'Table'[TRANSACTION_ID] )
            && 'Table'[SUBINVENTORY_CODE] = "L46"
    )
VAR PrevDate =
    MAXX ( PrevRow, [TRANSACTION_DATE] )
RETURN
    IF (
        'Table'[SUBINVENTORY_CODE] = "L46",
        DATEDIFF ( PrevDate, 'Table'[TRANSACTION_DATE], DAY )
    ) + 0

Total Days2 = 
SUM ( 'Table'[Days2] )

 

danextian_0-1734682299272.png

 

 





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

All the devices are from the Company and would not have a unique identifier. The Serial Number is the Devices ID where the C_Attribute1 is the Uniquer ID for the device in they system. 

 

The cronologicly by Transaction ID worked for the sample data I was working with to find a solution. But when I imported all 600,000+ line of the full data Power BI was unable to handle the calulations.

 

Thank you for your help.

lbendlin
Super User
Super User

Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).
Do not include sensitive information. Do not include anything that is unrelated to the issue or question.
Please show the expected outcome based on the sample data you provided.

Need help uploading data? https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-...
Want faster answers? https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447...

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.