Search code examples
powerbidaxpowerquerym

DAX & Power Query - Removing transaction line if sale is returned


First time posting, thanks in advance ! I have a simple sales table acting as a data source. In this table I have all my dimensions and facts, the data model is not organized as a star schema. I believe this question does not require the data model to be optimally built.

One column tracks the Sales Quantity and shows -1 if the item is returned.

In Power Query. I wish, for all returns, to eliminate the line with the -1, but also the associated line with the +1 sale.

I do not wish to simply filter by the Invoice Number and remove it, as in the same invoice I may have other items who were not returned.

I wish to have in my final table only final records.

Thank you

Did not manage to identify a way to tackle the issue in Power Query as of yet.


Edit, addition of further context Dataset abstract: I would like to keep the green row and remove the yellow ones. One invoice features three lines, two for one item being sold and returned, and one for an item being purchased for good. That latter one is to stay in the records.

[IMAGE : Excel version of relevant column in data base ]

https://i.sstatic.net/CiiiC.png


Solution

  • Because I do not know the exact nature of your data, I will give you a general solution for your problem, which will help you immensely.

    First, I will transform your data into facts and questions. Then I will answer the questions depending on my experience analyzing many different datasets; last but not least, I will provide you with the solution.


    Facts,

    1. [Sales Amount Include Tax] column values for Return Sales are negative numbers because excel format negative numbers by including them with parentheses, as your data shows.

    2. The Original Sale record and the Return Sale record must have the same values for these columns, which we will use to know the Original Sales record:

    The absolute value for the [Sales Amount Include Tax] column.

    [Invoice No], [Department], [Sub Dept], [Sub Dept GROUP], [Item Code] columns.

    This point is critical because we will use these columns to know the Original Sales record.

    Questions,

    1. Is the Item code column value similar to the original sale row and the returned sales row?

      My answer: It should be because you are returning the same item that the Item code column describes, but I am concerned that your data shows that the first two rows don't have the same Item code. Is it by mistake?

    2. Is the Sales Quantity column always filled with -1, even if the original sales quantity was, for example, three pairs of shoes?

      My answer: The Sales Quantity column should describe the number of items someone bought, so if I originally bought 2 things but didn't like them for a reason, I will return two items so that the column value will be -2.


    If my above assumptions are correct, you need to do the following in the Power BI query:

    1. Import your Sales table to Power BI. See this image that shows the test data I used. Sales excel data

    2. When you import your data and see it in the Power Query Editor, you will see that the Sales Amount Include Tax column value shows minus rather than parentheses for Return Sales Rows, as you can see in this image. Sales Power Query data

    3. Create a copy of your Sales table and Rename it to Returned Sales, then filter this table to include only [Sales Amount Include Tax] that are less than 0, as you can see in this image. Returned Sales - Power Query

    4. Filter the Sales table to include only [Sales Amount Include Tax] greater than or equal to 0, as you can see in this image. Sales First Filter- Power Query

    P.S. Now comes the fun part :)

    1. In Returned Sales table, add a custom column name it ReturnedSaleFlag, then give it a value of 1, as you can see in this image. Returned Sales ReturnedSaleFlag - Power Query

    2. Change ReturnedSaleFlag column type to Whole number.

    3. In Returned Sales table, transform [Sales Amount Include Tax] to an Absolute Value, as you can see in this image. Sales Amount Include Tax Absolute Value

    4. Now you need to return to the Sales table and merge it with Returned Sales table using multiple columns which are the column listed in the second point of the fact section, as the image shows. Also, see this link that describes how to Merge query based on multi columns. Merge Sales with Returned Sales Table using multiple column

    5. Expanded the new Returned Sales column in Sales table and only select ReturnedSaleFlag column and make sure to remove the check from use original column name as a prefix, as the image shows. Expanded Returned Sales

    6. Finally, all your previous hard work created a flag in your Sales table that will let you know the Sales record that had been returned. :)

    7. Now you filter the Sales table to keep the records that only have null values for the new ReturnedSaleFlag column then remove this column, as the image shows. Original Sales Filter

    8. In the end save and apply and analyze your Sales and your extra info in the Returned sales.


    I hope I helped in a way; if so, please mark this as an answer and vote for it :)