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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Excel and Power BI Query

Site NameTransaction_EndSales_Mix_IndPayment_MethodPayment_method_StdFuel or CRFuelCRESCard
Sedgemoor South20/05/2018 00:09FEuroshell CRT 2ES     
Sedgemoor South20/05/2018 00:09CBritish Pounds Sterlingcash     
Sedgemoor South20/05/2018 00:09CBritish Pounds Sterlingcash     
Sedgemoor South20/05/2018 00:55FMastercardcard00:4500:46   
Sedgemoor South20/05/2018 01:00FBritish Pounds Sterlingcash     
Sedgemoor South20/05/2018 01:46FEuroshellES00:4500:45 01:37 
Sedgemoor South20/05/2018 02:50FBritish Pounds Sterlingcash01:0301:03   
Sedgemoor South20/05/2018 03:30FVISAcard00:4000:40  02:34
Sedgemoor South20/05/2018 05:09FEuroshell CRTES01:3901:39 03:23 
Sedgemoor South20/05/2018 05:29CVISAcard  05:19  
Sedgemoor South20/05/2018 05:29CVISAcard    01:59
Sedgemoor South20/05/2018 05:38FBritish Pounds Sterlingcash     
Sedgemoor South20/05/2018 05:50CBritish Pounds Sterlingcash     
Sedgemoor South20/05/2018 05:50CBritish Pounds Sterlingcash     
Sedgemoor South20/05/2018 05:50CBritish Pounds Sterlingcash     
Sedgemoor South20/05/2018 06:16CBritish Pounds Sterlingcash     
Sedgemoor South20/05/2018 06:18CVISAcard    00:49
Sedgemoor South20/05/2018 06:18CVISAcard     
Sedgemoor South20/05/2018 06:18CVISAcard     
Sedgemoor South20/05/2018 06:32FAllStarcard 00:54   
Sedgemoor South20/05/2018 06:33CVISAcard     

 

Need help in automatically calculating the following columns in Excel and Power BI

 

 

The columns are "Fuel or CR", "Fuel", "CR", "ES", Card". In this example, I have manually calculated them, but I need the formula for the same.

 

 

This is how they need to be calculated


"Fuel or CR": This is straightforward. Difference between two rows of the Transaction_end column

F5 = B5 - B4
F6 = B6 - B5 
and so on and so forth
Note only values greater than 30 mins are mentioned here.

"Fuel",
This depends on the value in the field "Sales_Mix_Ind" depending on whether it is "F"
If its "F", that means Fuel, which means you only substract the corresponding values in the Transaction_end Column
Hence G5 = B5 -B2 as Sales_mix_indicator is "F" in columns C5 and C2
G7 = B6- B5 as Sales_mix_indicator is "F" in columns C6 and C5
Note only values greater than 30 mins are mentioned here.


"CR",

This depends on the value in the field "Sales_Mix_Ind" depending on whether it is "C"
If its "C", that means Fuel, which means you only substract the corresponding values in the Transaction_end Column

Hence H11 = B11 -  B4 as Sales_mix_indicator is "C" in columns C11 and C4
Note only values greater than 30 mins are mentioned here.


"ES",

This depends on the value in the field "Payment_method_std" depending on whether it is "ES"
If its "ES", that means EUroshell, which means you only substract the corresponding values in the Transaction_end Column

Hence I7 = B7 -  B2 as "Payment_method_std"  is "ES" in columns E7 and E2
And I10 = B10 -  B7 as "Payment_method_std"  is "ES" in columns E10 and E7
Note only values greater than 30 mins are mentioned here.


Card".


This depends on the value in the field "Payment_method_std" depending on whether it is "card"
If its "card", that means card payment, which means you only substract the corresponding values in the Transaction_end Column

Hence J9 = B9 -  B5 as "Payment_method_std"  is "card" in columns E9 and E5
And I12 = B12 -  B9 as "Payment_method_std"  is "card" in columns E12 and E9
Note only values greater than 30 mins are mentioned here.

 

 

3 REPLIES 3
v-shex-msft
Community Support
Community Support

Hi @Anonymous,

 

Your table seems contains duplicate records, I'd like to suggest you add a index column to calculate looping table.

 

Calculated column formulas:

Spoiler

 

Fuel or CR = 
VAR previous =
    CALCULATE (
        MAX ( Test[Transaction_End] ),
        FILTER ( ALL ( Test ), [Transaction_End] < EARLIER ( Test[Transaction_End] ) )
    )
VAR totalSecond =
    DATEDIFF ( previous, [Transaction_End], SECOND ) + 0
VAR difftime =
    TIME ( INT ( totalSecond / 3600 ), MOD ( totalSecond, 3600 ) / 60, MOD ( MOD ( totalSecond, 3600 ), 60 ) )
RETURN
    IF ( totalSecond >= 1800, difftime, BLANK () )


Fuel = 
VAR previous =
    CALCULATE (
        MAX ( Test[Transaction_End] ),
        FILTER (
            ALL ( Test ),
            [Index] < EARLIER ( Test[Index] )
                && [Sales_Mix_Ind] = "F"
        )
    )
VAR totalSecond =
    DATEDIFF ( previous, [Transaction_End], SECOND ) + 0
VAR difftime =
    TIME ( INT ( totalSecond / 3600 ), MOD ( totalSecond, 3600 ) / 60, MOD ( MOD ( totalSecond, 3600 ), 60 ) )
RETURN
    IF ( [Sales_Mix_Ind] = "F" && totalSecond >= 1800, difftime, BLANK () )


CR = 
VAR previous =
    CALCULATE (
        MAX ( Test[Transaction_End] ),
        FILTER (
            ALL ( Test ),
            [Index] < EARLIER ( Test[Index] )
                && [Sales_Mix_Ind] = "C"
        )
    )
VAR totalSecond =
    DATEDIFF ( previous, [Transaction_End], SECOND ) + 0
VAR difftime =
    TIME ( INT ( totalSecond / 3600 ), MOD ( totalSecond, 3600 ) / 60, MOD ( MOD ( totalSecond, 3600 ), 60 ) )
RETURN
    IF ( [Sales_Mix_Ind] = "C" && totalSecond >= 1800, difftime, BLANK () )


ES = 
VAR previous =
    CALCULATE (
        MAX ( Test[Transaction_End] ),
        FILTER (
            ALL ( Test ),
            [Index] < EARLIER ( Test[Index] )
                && [Payment_method_Std] = "ES"
        )
    )
VAR totalSecond =
    DATEDIFF ( previous, [Transaction_End], SECOND ) + 0
VAR difftime =
    TIME ( INT ( totalSecond / 3600 ), MOD ( totalSecond, 3600 ) / 60, MOD ( MOD ( totalSecond, 3600 ), 60 ) )
RETURN
    IF ( [Payment_method_Std] = "ES" && totalSecond >= 1800, difftime, BLANK () )


Card = 
VAR previous =
    CALCULATE (
        MAX ( Test[Transaction_End] ),
        FILTER (
            ALL ( Test ),
            [Index] < EARLIER ( Test[Index] )
                && [Payment_method_Std] = "Card"
        )
    )
VAR totalSecond =
    DATEDIFF ( previous, [Transaction_End], SECOND ) + 0
VAR difftime =
    TIME ( INT ( totalSecond / 3600 ), MOD ( totalSecond, 3600 ) / 60, MOD ( MOD ( totalSecond, 3600 ), 60 ) )
RETURN
    IF ( [Payment_method_Std] = "Card" && totalSecond >= 1800, difftime, BLANK () )

 

24.png

 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.
Anonymous
Not applicable

Hi Xiaoxin Sheng

 

     Many Thanks for your reply. Your reply has allowed me to get the desired result on a small subset of the data that I had mentioned. However if I try to apply the same logic on a much bigger data set of 1 month (366856 rows), I am getting an out of memory error. I try to create a new column after applying an index on the data but the column creation "keeps working on it" for a long time and gives me an out of memory error.

Is there a way I can resolve this issue.

 

I have modified your code slightly. In fact I also had devised another method of achieving the same result, before your reply, but both methods are giving me an out of memory error.

 

 


Fuel2 =
VAR previous =
    CALCULATE (
        MAX ( Original[Transaction_End] ),
        FILTER (
            ALL ( Original ),
            [Index] < EARLIER ( Original[Index] )
                && [Sales_Mix_Ind] = "F"
        )
    )
VAR totalminute =
    DATEDIFF ( previous, [Transaction_End], MINUTE ) + 0
RETURN
    IF ( [Sales_Mix_Ind] = "F" && totalminute >= 60, totalminute, BLANK () )

 


LagColumn F = DATEDIFF(Original[Transaction_end]
                , CALCULATE(MAX(Original[Transaction_end]),
                    FILTER(original,original[Sales_Mix_Ind]="F"),
                    FILTER(Original,
                        Original[Sales_Mix_ind] = EARLIER(Original[Sales_Mix_ind]) &&
                        Original[Transaction_end] < EARLIER(Original[Transaction_end])
                    )
                )
            , MINUTE
            )

 

Would be great if you can help me resolve this issue

 

Rgds

Amit

 

 

 

Hi @Anonymous,

 

Which version of power bi desktop you used? If you are working on 32 bit version, I'd like to suggest you use 64 bit to increase application memory limit.(32 bit application has 2G memory usage limit)

 

In addition, your calculation need to looping whole table to compare current and previous records to get diff. It obviously cost lots of memory resource when you use a lot similar calculations formulas.

 

For this scenario, I'd like to suggest you add more conditions to reduce loop range to optimization memory usage.

 

Regards,
Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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