Thursday, July 23, 2020

Power BI Turns 5 and I turn 3

When I was a kid, my favorite cartoon show was ‘Captain Planet’. For those who do not know about Captain Planet here is a link to its “Intro Theme”. 

Power BI has really been 'Captain Planet' for all the business users who want to be ‘Data Literate’. It is amazing how the Power BI team has democratized Business Intelligence enabling business teams toward Data Literacy and making data informed decisions.

Learning about the product has been so smooth with all the tips and tricks provided by Enterprise DNA, Reza Rad, SQL BI ,Guy in a Cube, Curbal, BI Elite and many more.

Also, a special thanks to the vibrant community who work day and night to solve the issues and challenges posted. It is truly a #pbifamily.

And yes, it is my 3rd month birthday on Power BI. I was introduced to Power BI 3 months ago and I somewhat have fell in love with it.

And it's time to celebrate Power BI’s 5th Birthday. And that is why I have tried creating a dashboard based on the  challenge  at the Enterprise DNA Forum.

Brief Problem Description: Your consultancy has implemented an app which scans labels on parcels for a client to help them improve their delivery process and fulfillment process. The management now wants to evaluate how business has reacted and how effective the app is.

I tried creating a Bee Themed Template (resembling to Power BI Colours) and SVG to fill the bees from the blog by Laura GB. Thanks Laura :)

Do let me know your views.





Cheers,

Harsh 


Wednesday, June 3, 2020

Find Previous Details of a Customer in Power BI

Calculate Values from Previous Row

 

Harsh is a Sales Manager at XYZ Corporation. While looking at the Sales Report, he wanted to check the numbers of days between 2 orders from a customer. He also wants to know the previous order (Order Date, Order No, Sales Amount) details in the same row.

While this may seem a very easy solution in Power BI, it is also one of the most frequently asked questions in the official Power BI community.

So, let’s try and solve this using 2 approaches.

 


Order Date (Date/Time), Customer Id (Text), Sales Order No (Text), Sales Amount (Numeric).

As soon as an order is placed, a Sales Order No gets generated.

 

CALCULATED COLUMNS

 

The idea here is to get the previous Sales Order Date, Sales Order Number and Sales Amount aligned to next purchase of the customer.



You can use the below formula to do so


Previous Order Date = 

CALCULATE (

    MAX ( 'Table'[Order Date] ),

    FILTER (

        ALLEXCEPT (

            'Table',

            'Table'[Customer ID]

        ),

        'Table'[Order Date]

            < EARLIER ( 'Table'[Order Date] )

    )

)


Similarly, since the Sales Order No is generated sequentially, you can use the similar formula.


 Previous Sales Order No = 

CALCULATE (

    MAX ( 'Table'[Sales Order Number] ),

    FILTER (

        ALLEXCEPT (

            'Table',

            'Table'[Customer ID]

        ),

        'Table'[Order Date]

            < EARLIER ( 'Table'[Order Date] )

    )

)




Since Dates and Sales Order No are sequential hence it was easy to shift the dates. The Max (Sales Order No) of the filtered table always gave you the previous value of the Sales Order No.

 

For Example,

Suppose the row marked in Black is the current row which is under evaluation, the table above (marked in green) gets filtered due to the value in the filter context ('Table'[Order Date] < EARLIER (Table [Order Date]) and the value returned is MAX(Order Date) which is 29-Jan-20 1:30:00 PM in this case.



But the Sales Amount is not sequential value. If we were to follow the same step as above, the Previous Sales Amount will return $1,192.04 while the correct value which should be returned is $200.

 

To overcome this, one way is to create an Index Column. We can Power Query and create one. But, here is a method incase you want to do this through a Calculated Column.

 

You can use the RankX function to create an index Column

Rank Date =

VAR _rankSalesOrder =

    RANKX (

        FILTER (

            ALL (

                'Table'[Order Date],

                'Table'[Customer ID],

                'Table'[Sales Order Number]

            ),

            'Table'[Customer ID]

                = EARLIER ( 'Table'[Customer ID] )

        ),

        'Table'[Order Date],

        ,

        ASC

    )

RETURN

    _rankSalesOrder


The formula ranks the customer id based on Order Date.

 



With the help of this column we can now, find the previous Sales Amount.


 Previous Sales Amount = 

CALCULATE (

    MAX('Table'[Sales Amount]),

    FILTER (

        ALLEXCEPT (

            'Table',

            'Table'[Customer ID]

        ),

        'Table'[Order Date]

            < EARLIER('Table'[Order Date])  && 'Table'[Rank Date] = EARLIER('Table'[Rank Date]) - 1

    )

)




Now, we can calculate the difference in Days between 2 orders.


Difference in Days = DATEDIFF('Table'[Previous Order Date],'Table'[Order Date],DAY)



PS: This is just one way to solve this kind of problems.



Cheers,

Harsh Nathani

 

 


Power BI Turns 5 and I turn 3

When I was a kid, my favorite cartoon show was ‘Captain Planet’. For those who do not know about Captain Planet here is a link to its “Intro...