Dax - Creating Column That Checks Dates Sequence - powerbi

I have im my model table that contains data from reports based on monthly reporting of employees with column names "ReportDate" and enployye numbers.
I want to check that there is no gaps between the monthly dates to each employee with DAX.
For example:
EmpNum | ReportDate | CheckColumn
111 | 30.08.2019
111 | 30.09.2019
111 | 31.10.2019
222 | 30.08.2019
222| 31.10.2019 ----------> Here I want alert in my CheckColumn
Can someone find me a solution?

First you need to create a index column. Go to Edit Queries > Add Column > Index Column, starting with 1 for example.
Next you add a column with DAX which has a shift of 1 to the original column with this expression (make sure this column is from the same date format as your original column; Modelling > Format):
ShiftColumn = DATEVALUE(CALCULATE(MAX('Table'[Report Date]);FILTER('Table';'Table'[Index]=EARLIER('Table'[Index])-1)))
Next add the column with the check:
Column 2 = IF(DATEADD('Table'[Report Date].[Date];-1;DAY) = 'Table'[ShiftColumn]; TRUE(); FALSE())
The result:

Related

Extract values from column to use in calculated measure formula

I have TABLE A
In that table I have a measure with values like so:
Targets|
--------
4 |
5 |
6 |
In the same table I have a calculated column (summed totals) like so:
Totals |
--------
10 |
11 |
12 |
Because this is a direct query data source, query editor is disabled and manipulation must be done through DAX formulas.
I would like to do a simple operation of Targets-Totals
Code I've tried for a calculated column:
test = TableA[targets] - TableA[totals]
However this results in an error:
The column TableA[test] cannot be pushed to the remote data source and cannot be used in this scenario.
How can I create a new column with the above operation considering the fact that one column is a 'measure ' and the other a 'calculated column'
In this case, you will need a measure that does a row by row calculation, but not as a calculated column. For this you will need SUMX which will do a iteration.
Your measure should be:
New Measure = SUMX(TableA, [targets] - [totals])

Power BI calculate sum only last value of duplicate ID

I'm struggling to create a Measure that sums a column and have it filter out duplicate IDs while taking only the latest row.
For example, there is a table as such:
UID | Quantity | Status | StatusDate
aaa | 3 | Shipped | 11/1/2020
aaa | 3 | Delivered | 11/5/2020
bbb | 5 | Ordered | 10/29/2020
ccc | 8 | Shipped | 11/4/2020
So the idea would be to sum the quantity, but I would only want to count quantity for id "aaa" once and only count towards the latest status ("Delivered" in this case). I would make a visual that shows the quantities with status as its axis. I also need to add a date Slicer so I could go back in time. So, when I go before 11/5/2020, instead of "Delivered," it would switch back to "Shipped."
I tried several methods:
SUMMARIZE to a table filtering "MAX" date value if UID is the same. I found this doesn't work with the date slicer since it seems like it is not actually recalculating the filtering and just slicing away rows outside of the dates. Seems to be the same whether the SUMMARIZE is set as a new table or VAR in the Measure.
CALCULATE seems promising but I can't seem to figure out a syntax
that filters that I need. Example of one that doesn't work (I also tried SUMX instead of SUM but that doesn't work either):
CALCULATE(
SUM(Table1[Quantity]),
FILTER(Table1, [StatusDate]=MAXX(FILTER(Table1,[UID]=EARLIER([UID])),[StatusDate])
)
I also tried adding a column that states whether if the row is "old" as well as a numerical "rank" to the different statuses. But once again, I run into the issue where the date slicer is not exactly filtering to recalculate those columns. For example, if the date slicer is set to 11/3/2020, it should add "3" to "Shipped" instead of "Delivered." But instead of that, it just removes the row which tells me that it is not actually recalculating the columns (like #1).
Any help would be appreciated :-) Thank you!
You can try something like this:
Measure =
VAR d = LASTDATE(Table1[StatusDate])
VAR tb = SUMMARIZE(FILTER(Table1, Table1[StatusDate] <= d),
Table1[UID],
"last", LASTDATE(Table1[StatusDate]))
RETURN CALCULATE(SUM(Table1[Quantity]), TREATAS(tb, Table1[UID], Table1[StatusDate]))
The tb variable contains a table which has the latest date per UID. You then use that to filter your main table with the TREATAS function.
One other alternative is to create a table with the RANK function ordered by date and then doing a SUM over that table, where Rank = 1.

PowerBI DAX - Filter Total based on selected columns

I have the table that looks like this:
Column Header: User | Test Score 1 | Test Score 2 | Test Score 3 | Total Points | Status
Row: Person1 | 50 | 70 | 75 | 195 | Failed
Row: Person2 | 70 | 75 | 85 | 210 | Passed
The "Total Points" column is simply the SUM of the three test scores and the "Status" is calculated based on the "Total Points" (if "Total Points" < 200, "Failed", "Passed").
What I'm having difficulty with is that sometimes a test needs to be eliminated from the equation. I would like the end user to be able to uncheck a box in the filters area or on a slicer to remove a test from the equation for the "Total Points", which would then affect the "Status". Since the test is a column header and not a value, I can't seem to find a way to make this work.
The data table is already in a report layout. That makes your intention difficult to process. A better layout would be a flat table in this format:
Name, Test number, Score
You can use Power Query to unpivot the data to get from your layout to the flat table. With a flat table, you can then build a measure for the total and add a slicer to the report where the test number can be selected.
Build a matrix visual with the Name in the rows and the test number in the columns, the measure for the total in the values. Then use the slicer to remove tests from the matrix at your discretion.

Split data into categories in the same row in Power BI

I have a table that contains multiple columns with their named having either the suffix _EXPECTED or _ACTUAL. For example, I'm looking at my sold items from my SoldItems Table and I have the following columns: APPLES_EXPECTED, BANANAS_EXPECTED, KIWIS_EXPECTED, APPLES_ACTUAL, BANANAS_ACTUAL, KIWIS_ACTUAL (The Identifier of the table is the date, so we have results per date). I want to show that data in a table form, something like this (for a selected date in filters:
+------------+----------+--------+
| Sold items | Expected | Actual |
+------------+----------+--------+
| Apples | 10 | 15 |
| Bananas | 8 | 5 |
| Kiwis | 2 | 1 |
+------------+----------+--------+
How can I manage something like this in Power BI ? I tried playing with the matrix/table visualization, however, I can't figure out a way to merge all the expected and actual columns together.
It looks like the easiest option for you would be to mould the data a bit differently using Power query. You can UNPIVOT your data so that all the expected and actual values become rows instead of columns. For example take the following sample:
Date Apples_Expected Apples_Actual
1/1/2019 1 2
Once you unpivot this it will become:
Date Fruit Count
1/1/2019 Apples_Expected 1
1/1/2019 Apples_Actual 2
Once you unpivot, it should be fairly straightforward to get the view you are looking for. The following link should walk you through the steps to unpivot:
https://support.office.com/en-us/article/unpivot-columns-power-query-0f7bad4b-9ea1-49c1-9d95-f588221c7098
Hope this helps.

How to use a Power BI slicer to filter two column of a table

I have a table of Enter and Exit time of some people like this:
Name | EnterTime | ExitTime
=============================
Tom | 13:52:00 | 20:55:00
Robert | 15:55:00 | 21:12:00
Sarah | 19:08:00 | 19:48:00
Jack | 16:54:00 | 17:32:00
I want to show the user that in a selected point of time who has presence.
In other word, I want to have a dateTime slicer that the user will pick a time and then the table got filtered to show only rows with:
EnterTime < selectedTime < ExitTime
This can be done with a few steps.
First, we need a list of dates to go into our slicer. If you're using a separate date table, then that's a great source. Otherwise, you can build one with DAX that will contain all the dates in your EnterTime and ExitTime columns.
Click on 'Create Table' in the modeling ribbon. Put in the following DAX:
DateListTable = UNION(
SELECTCOLUMNS('MyTable', "DateList", 'MyTable'[EnterTime]),
SELECTCOLUMNS('MyTable',"DateList", 'MyTable'[ExitTime])
)
You now have a slicer and a table visual. Select the slicer and then navigate into the Format Ribbon (it's only available when the visual is selected. Use Edit Interactions to turn off the Slicer's filtering on the table. Set it to no interaction (the circle with a diagonal line through it).
Create a new measure
ShowDate =
VAR SelectedDate = SELECTEDVALUE('DateListTable'[DateList], BLANK())
Return
if(SelectedDate >= Max('MyTable'[EnterTime ]) && SelectedDate <= max('MyTable'[ExitTime]), 1, 0)
Finally, filter your table visual on the new measure, ShowDate, for when it is 1.
So, we create an independent slicer with a complete list of dates. But we break any filter relationship between it and the table. Instead, we use 'SelectedValue' to capture the value chosen from the filter, and create a dax measure that shows '1' when the value in the filter is between EnterTime and Exit time. Filter on that dax measure, and we get our desired behavior.