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

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.stack.imgur.com/CiiiC.png

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,
[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.
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,
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?
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.
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.
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.
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.
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.
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.
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.
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.
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.
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 :)

Related

Create Custom Column with IF and lookup functionality to own table

I have merged two tabels ( sales and forecast ). For all the rows coming from sales query the cost price column has a value. The forecast rows does not have that.
In order to calculate future metrics/KPI I need to make a Power Query transformation that populates cost price on all forecast rows. I would like to do some kind of refence to the ProductName (exits both on the sales and forecast rows) and pull the cost price from the sales rows. The ProductName can have multiple entries in the table, but will be the same for alle the rows. So maybe a find first/max or something would be fine.
However, I am not sure have to make this calcuated column with some sort of lookup to ProductName?
Well you can definitely do so
Here is an excellent Article form Microsoft on LookupValue
In addition check this Thread as well. It will give you more Idea.
I would do something like
=LOOKUPVALUE(Product[SafetyStockLevel], [ProductName], " Mountain-400-W Silver, 46")

Create Calculated Table in Power BI based on the earliest date that another criteria is met on

I am trying to create a calculated table in Power BI that will provide one row per person based on the most earliest date that the score column is greater than 9. The initial table is thousands of rows and includes multiple entries per person if more than one score was collected.
Initial table:
Output goal:
Since Supergirl didn't ever have a score > 9, she isn't included in the output. Each person with a score > 9 will have one row with the score that occurred on the first date that the score was > 9 and the Date Completed.
I will then add columns and measures based on this new table. I tried using the summarize function and duplicating the initial table and manipulating it without success and have read many posts and didn't find anything that matched my ask. Please help :)
you could start off with creating an additional calculated column "TenOrMore" where you would simply calculate if Score >= 10 then true else false. Then your next select you can do max or min on the score where column "TenOrMore" is true or select all the values and calculate the next step. I hope this helps.

Power BI cumulative count with multiple condition

Did anyone know how to convert RunningCount into Power bi Dax ?
I test on RunningTotal, Rankx but seems not working.
The [year] is just a text column not in Datetime format.
I still new on this and not sure if my explain is good enough or not. Sorry for any inconvenient cause.
I try to create measure/ calculation column in power bi from the formula below.
I need the count by Year, break by column like product, customer, rate and category.
Before I get into my solution, there are a few assumptions I had to make:
The "Year" column is supposed to be years, and not five digit numbers. So in my data I dropped the second "2" in each (i.e. "20213" -> "2013").
You may have another column in your data to break ties, but given the data you provided, there is no way to rank the first and third lines (they both have product ABC and year 2003).
Given those assumptions, here is my solution...
First, here is what my data looks like. I added an ID column so that we can see every row, even duplicates.
From there, you can simply add a new column with the following formula.
Running Count =
COUNTROWS(
FILTER(
'Data',
[ProductName] = EARLIER([ProductName]) &&
[Customer] = EARLIER([Customer]) &&
[Seller] = EARLIER([Seller]) &&
[Year] <= EARLIER([Year])
)
)
The EARLIER function is being used to specify a ProductName, Customer, etc. from the row of the table to be used in the filtering of the data. Once we have the data filtered, we can simple count the number of rows.
The final result looks like this. As noted in my second assumption, there is no way to break ties, so my numbers are slightly off from what you have in your screenshot

DAX CALCULATE() doesn't seem to be overriding pivot filter

I am learning to use Power Pivot and write DAX and I am working through a book I bought written by Rob Collie and Avichal Singh called "Power Pivot and Power BI".
Early on it explains what happens when using CALCULATE() in a measure. A key point of understanding is explained as follows:
If a filter argument acts on a column that IS already on the pivot, it
will override the pivot context for that column
So in a simple table called "GSR" I have a series of invoices with an invoice date, a product and an amount. I have another column that converts all invoice dates to the last day of the month to gather them together. I have created a measure called "Total orders" that is counting the number of rows.
I have created a pivot of this data with:
Products in the rows
Month end in the columns (but I've actually chosen Month and Year from the automatic breakdown Power Pivot has done on my Month_End column)
Orders in the values
This pivot renders correctly.
Now the issue:
I've created two slicers that are feeding off two disconnected tables; one containing month numbers, and one containing years. Based on the selection from each of these I have a measure that creates a month end using the EOMONTH(DATE(),0). This measure is called "Comparison_Month_End".
I then have another measure called "Compare_Orders" that contains the following:
CALCULATE([Total orders],FILTER(GSR,GSR[Month_End]=[Comparison_Month_End]))
The point of all of this - this is meant to get the orders from the GSR where the month and year match the slicer selection. I want this value to appear in the pivot for every month selected i.e. not filtered by the pivot.
It doesn't work, however. This seems to me to be counter to what the book says, which is that if the filter in the measure is applied to a column on the pivot (in this case Month_End), the pivot filter is overriden. So, for example, if I have 31-Mar-18 in a column on my pivot and the comparison month end is 31-Jan-18, I would expect to see the January orders within "Compare_Orders" sitting next to the March orders, the February orders, the December orders and so on, but it only appears next to January.
To me this is doing exactly the same as an example in the book where there is a pivot with Year in the rows, Total Sales in column 1 and then 2002 sales in column 2. Column 1 shows sales for each year, column 2 shows the same sales figure (i.e. 2002 sales) against every row, even where the row year is 2001, 2003 or 2004. The 2002 Sales measure is using CALCULATE() summing Total Sales, filtering on "Sales[Year] = 2002".
Could anyone please explain why what I expected to happen doesn't seem to be happening please?
Thank you.
CALCULATE does indeed override the filter context when evaluating its first argument; using the rows provided to its second argument as the new filter context. However; CALCULATE does not alter the filter context when evaluating its second argument, and so pre-existing filters remain unless they are explicitly removed.
In FILTER(GSR,GSR[Month_End]=[Comparison_Month_End]), FILTER only iterates over rows of GSR that are present in the pre-existing filter context (the pivot context). To get all rows, use ALL:
CALCULATE([Total orders],FILTER(ALL(GSR),GSR[Month_End]=[Comparison_Month_End]))

Sharepoint calculated column date calculation

I have a list in SharePoint Office 365 with a number of columns to register and track claims.
One of the columns is a calculated one - to catch the duration of claims processing.
I would like to calculate it as a difference between dates and shown in days.
To do it I want to use 3 pieces of information:
Column [RegistrationDate]
Date of new registered claim.
Column [EndDate]
Finalization of the claim.
[Today]
Sytem date for now.
The logic to be done by the formula should be:
IF [EndDate] is empty THEN:
[Today]-[RegistrationDate]
OTHERWISE
[EndDate]-[RegistrationDate]
Question: What formula will achieve my desired output?
Calculated columns cannot use the [Today] dynamic variable. (Validation formulas can.) Calculated Columns are only updated when they are edited, not each time they are viewed.
If you could use [Today], it would look like this:
=IF( ISBLANK( [EndDate] ), [Today]-[RegistrationDate], [EndDate]-[RegistrationDate] )
But... you cannot use [Today] in a calculated column...