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

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.

Related

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.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 :)

Calculated column in Power BI based on relationship between 2 tables

Hi,
I'm trying to add a calculated column in my MergedTable table that will multiply the Time column by the sum of the indicator in the TeamLeave when the week ending dates and the name dates are the same but I'm not sure how or what functions to use(I've tried a number at this stage).
Could anyone provide me with assistance on this?
Thanks,
This:
'MergedTable'[Time] * CALCULATE(SUM('Team Leave'[Indicator]))
The CALCULATE is necessary to perform a context transition (turning the "current row" of 'MergedTable' into a filter that will propagate to the 'Team Leave' table).

Dax Finding date based on Criteria calculated Column

I couldn't find an answer for my issue elsewhere, so trying my luck here.
I have sales table and my final result should determine if there were sales made for same person in specific period of time, for example within 7 business days. for example:
For ID 123 I have to flag it that sale for products A,B,C where within specified period.
For ID 1234 only sales of products A and B meet the criteria product C was sold in irrelevant time frame.
I've created a date table with indicators that determine for each date if the date is a working day, but i am struggling to calculate the relevant last working day
For example: I need that for 01/01/2019 i will get 10/01/2019 date, based on NUMOFDAYS and FinalWorkday = TRUE, which basically means that i have to count NUMOFDAYS times TRUE statement for each date and return corresponding date.
After that step done I think that it would be pretty easy for me to determine if the sale for a person was made in specific time frame.
If someone can give me direction for that much appreciated
Thank you in advance
You could use a DateTable like this one:
I used the following DAX-expressions for the calculated columns:
nrDays = 7
isWorkDay = WEEKDAY('DateTable'[Date],2) < 6
rankWorkingDays = RANKX ( FILTER ( DateTable, DateTable[isWorkDay] = TRUE () ),
DateTable[Date] , , ASC )
LastWorkDay = LOOKUPVALUE ( DateTable[Date],
DateTable[isWorkDay], TRUE (),
DateTable[rankWorkingDays], DateTable[rankWorkingDays] + DateTable[nrDays])
This issue can be solved by the following, since three are non-working days/holidays we can filter them out via POWERQUERY, than add an Index Column and Another column Which is basically Index column + Number of days wanted, then simply merge duplicate of dates query on Index+number of days wanted column on Index column. And we get the correct date

Split single row of data in Power BI data source into multiple rows

I have a table in a Power BI data source with a column for term start and term end date (term length can be longer than a month), along with meta data on the term. I need to report on status of purchased terms as at the end of each month. As far as I can see, the best way of accomplishing this would be to create a calculated table with an entry for each month on which a term is active at its end.
For example, an entry in the original table with the following data:
TermStartDate TermEndDate PurchaseAmount
2018-01-03 2018-04-12 100
Would end up in the calculated table as follows:
MonthPurchased PurchaseAmount
2018-01 100
2018-02 100
2018-03 100
How to accomplish this? Is there a better way than creating a separate calculated table to get this data? Any help or advise is appreciated
I managed to solve this myself, I detail the required steps below for reference:
Change start and end date column data types from Datetime to Date. <- This is needed to ensure we only generate dates on day boundaries in the next step
Add custom column with the following formula:
Month = List.Select( List.Dates([TermStartDate], Number.From([TermEndDate] - [TermStartDate]) +1, #duration(1, 0, 0, 0)), each _ = Date.EndOfMonth(_) )
This generates a list of dates between start and end, then filters to only leave the dates that are at the end of a month
Expand to new rows on the new Month column (menu at the top of the column)
Use Detect Data Type option on the Month column to change the datatype from Any to Date (for some reason I cannot manually select Date, the DataType menu option is greyed out on the Month column)

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]))