Filter existing table to another table without adding measures or column on existing table - powerbi

I want to create a table based on input table.
Input table is:
The new table filters the input table to show the last entry of every day.
I have tried working with measure but sometimes cant tell if it is working right until I graph it in pivot tables which is not so bad but sometimes just doesn't show me what I need to see exactly.
I have tried this measure:
History_Daily Efficiency =
VAR LastDailyEfficiency =
GENERATE(
VALUES ('Table_Full'[Cell]),
CALCULATETABLE (
TOPN (
1,
GROUPBY (
'Table_Full',
'Table_Full'[Date],
'Table_Full'[Time],
'Table_Full'[Efficiency]
),
'Table_Full'[Date], DESC,
'Table_Full'[Time], DESC,
'Table_Full'[Efficiency], ASC
)
)
)
RETURN
CALCULATE (
AVERAGE('Table_Full'[Efficiency]),
TREATAS( LastDailyEfficiency, 'Table_Full'[Cell], 'Table_Full'[Date], 'Table_Full'[Time], 'Table_Full'[Efficiency]),
'Table_Full'[Efficiency] < 80
)
But I got this:
I would like to see this as the output:

You can create a new table:
LastDayCount = GROUPBY(Table_Full;Table_Full[lob/Part Number];Table_Full[Date];"LastDate";MAXX(CURRENTGROUP(); Table_Full[DateTime]))
This will create a table with the last DateTime of the day.
Next we add a column giving us the max of that particular last datetime of the day. I noticed that you have more the same entries, the logic below takes the max part count at the end of the day when more than one entry.
Count =
CALCULATE(MAX(Table_Full[Part Count]);
FILTER(Table_Full;LastDayCount[Table_Full_lob/Part Number] = Table_Full[lob/Part Number]
&& LastDayCount[LastDate] = Table_Full[DateTime]))
End result:

Related

Set First day of the range as Initial Inventory + Production - Sales = Inventory (DAX)

My challenge has been how to
01 - set Initial Inventory (considering the first day of the column);
02 - From the second day on, have Forecast (Previsão) - Sales (Vendas) = Balance. So 3 columns for each day
The way it is now, it repeats Initial Inventory throughout the days.
Here's the goal I'm trying to achieve:
Here's how the tables are related:
I would try to approach it with an unrelated helper table, that would contain 2 columns and 4 rows. Here's an example based on Contoso data.
Here's the helper table. Column Item ID is used for sorting result columns as well:
And now comes the magic:
Helper measure =
VAR minDate =
CALCULATE ( MIN ( DimDate[Datekey] ), ALLSELECTED ( DimDate ) )
VAR curDate =
SELECTEDVALUE ( DimDate[Datekey] )
RETURN
SWITCH (
SELECTEDVALUE ( Helper[Item ID] ),
1, IF ( curDate = minDate, [Opening Inventory Qty], BLANK () ),
2, [Production Qty],
3, - [Sales Qty],
4, [Closing Inventory Qty]
)
Here's an explanation:
minDate is used to get initial date on the visual
curDate is used to get current date
Next I check, which column from the helper table the measure is calculated for using SWITCH function
using SWITCH, I return different formula, based on helper column type
for Opening inventory, I only return a value when current date is equal to initial date. Otherwise, I return BLANK() - that's why Opening inventory only appears on the first date
The matrix visual is built using Product name in rows; Year, month, day and Item (from the helper table) in columns, and the Helper measure from above.
Here's a the result (don't mind quantities, it's a sample data):
EDIT:
For completeness' sake, here's how the model looks. Notice that the Helper table is not related to any other table:

Count unique matching items as a calculated column

I have two tables are Data and Report.
Data Table:
In Data table contain two columns are Item and status.
The item column contains duplicated entry and the item column contains text and number or number only or text only.
The status column contains two different text/comments, "Okay" and "Not Okay"
The report table
In the Report table, I updated both comments/text as "Okay" or "Not Okay".
I would like to create a new calculated column in the report table in order to get the unique count according to the comments based on the data table columns item and status.
In Excel, I am applying the following formula
F2=SUM((FREQUENCY(MATCH(A$2:$A$19&"",$A$1:$A$19&"",0)*($B$2:$B$19=$D3),ROW($A$2:$A$19))>0)+0)-1
in order to get my final result.
I don't want measure solutions.
DATA TABLE:
REPORT TABLE:
EXCEL LOGIC:
This is much easier in DAX than in Excel and there are many ways to do it.
Here are some possibilities with different approaches:
Desired Result =
VAR Comment = REPORT[COMMENTS]
RETURN
CALCULATE (
DISTINCTCOUNT ( DATA[ITEM] ),
DATA[STATUS] = Comment
)
Desired Result =
COUNTROWS (
SUMMARIZE (
FILTER ( DATA, DATA[STATUS] = REPORT[COMMENTS] ),
DATA[ITEM]
)
)
Desired Result =
SUMX (
DISTINCT ( DATA[ITEM] ),
IF ( CALCULATE ( SELECTEDVALUE ( DATA[STATUS] ) ) = REPORT[COMMENTS], 1, 0 )
)

How to produce a snapshot table using Power BI measure

My intention is to populate days of the month to simulate a data warehouse periodic snapshot table using DAX measures. My goal is to show non-additive values for the quantity.
Consider the following transactions:
The granularity of my snapshot table is day. So it should show the following:
Take note that a day may have multiple entries but I am only interested in the latest entry for the day. If I am looking at the figures using a week period it should show the latest entry for the week. It all depends on the context fixter.
However after applying the measure I end up with:
There are three transactions. Two on day 2 and the other on day 4. Instead of calculating a running total I want to show the latest Qty for the days which have no transactions without running accumulating totals. So, day 4 should show 4 instead of summing up day 3 and day 4 which gives me 10. I've been experimenting with LASTNONBLANK without much success.
This is the measure I'm using:
Snapshot =
CALCULATE(
SUM('Inventory'[Quantity]),
FILTER(
ALL ( 'Date'[Date] ),
'Date'[Date] <= MAX( 'Date'[Date] )
)
)
There are two tables involved:
Table # 1: Inventory table containing the transactions. It includes the product id, the date/time the transaction was recorded and the quantity.
Table # 2: A date table 'Date' which has been marked as a date table in Power BI. There is a relationship between the Inventory and the Date table based on a date key. So, in the measure, 'Date'[Date] refers to the Date column in the Date table.
You can use the LASTNONBLANKVALUE function, that returns the last value of the expression specified as second parameter sorted by the column specified as first parameter.
Since LASTNONBLANKVALUE implicitly wraps the second parameter into a CALCULATE, a context transition happens and therefore the row context is transformed into the corresponding filter context. So we also need to use VALUES to apply the filter context to the T[Qty] column. The returned table is a single row column and DAX can automatically convert a single column, single row table to a scalar value.
Then, since we don't have a dimension table we have to get rid of cross-filtering, therefore we must use REMOVEFILTERS over the whole table.
the filter expression T[Day] < MaxDay is needed because LASTNONBLANKVALUE must be called in a filter context containing all the rows preceding and including the current one.
So, assuming that the table name is T with fields Day and Qty like in your sample data, this code should work
Edit: changed in order to support multiple rows with same day, assuming the desired result is the sum of the last day quantities
Measure =
VAR MaxDay =
MAX ( T[Day] )
RETURN
CALCULATE (
LASTNONBLANKVALUE (
T[Day],
SUM ( T[Qty] )
),
T[Day] <= MaxDay,
REMOVEFILTERS ( T )
) + 0
Edit: after reading the comments, this might work on your model (untested)
Measure =
VAR MaxDay =
MAX ( 'Date'[Date] )
RETURN
CALCULATE (
LASTNONBLANKVALUE (
Inventory[RecordedDate],
SUM ( Inventory[Quantity] )
),
'Date'[Date] <= MaxDay
) + 0

DAX sum filtered by multiple columns of related tables

I have a measure, which is being added to a table and a Card. The measure is used to show the total hours posted where Calls.[Sch Engineer]=Hours.Employee AND Calls.ProjID=Hours.ProjID
The value on each row of the table is correct. However the total value for this measure is incorrect in the table and also the Card, and I cant work out why.
So the measure is:
SchEngHrsOnly =
VAR main =
MAX ( Calls[Sch Engineer] )
RETURN
CALCULATE ( SUM ( 'Hours'[Hrs] ), Hours[Employee] = main )
When I add this to a table visual I get the following - both Table total and Card value for measure are incorrect - they should be 163.50:
If I select a row in the table visual, then the Card visual shows a correct value, otherwise it shows an incorrect value:
My data model is:
My relationships are:
What I am looking to get is:
Also, please see here the PBIX file:
PBIX File
Can anyone help with this issue please?
If you want SUM of MAX values, then go this way:
SumOfMaxes =
SUMX(
VALUES( Hours[ProjID] ),
CALCULATE( MAX( Hours[Hrs]) )
)
It produces:
You might be also interested in:
DAX ALLEXCEPT to sum by category of multiple dimension tables
DAX Median of category sums
Edit
After your explanations I see that you want filtered sum.
FilteredSum =
CALCULATE (
SUM ( Hours[Hrs] ),
FILTER (
Hours,
Hours[Employee] = RELATED ( Calls[Sch Engineer] )
&& Hours[ProjID] = RELATED ( Calls[Proj ID] )
)
)
https://www.sqlbi.com/blog/marco/2010/02/09/how-to-relate-tables-in-dax-without-using-relationships/
You can create it as a calculated column and then sum it up to get the value:
Hours Calc = CALCULATE(MAX(Hours[Hrs]),FILTER(Hours,Hours[ProjID]=Calls[ProjID]))
The above calculation will add a column with the maximum of hours for each project ID. Then you can sum it up in the final table and should get the desired results. Hope this helps.

Count datetime intervals which contains timestamp with DAX

I have two tables:
A calendar table with both dates and hours.
And a table that contains incidents, with start time and end time in addition to set of attributes (only one, incident_code, in the example table).
For a specific date range, I want to show how many incidents occurred each hour.
The following measure works, but struggles when the incident table becomes large and there is several slicers
incidentCnt =
CALCULATE(
COUNTROWS(incidents);
FILTER(
incidents;
incidents[start_datetime] < MAX(date_hour[datetime]) &&
incidents[end_datetime] > MAX(date_hour[datetime])
)
)
What would be a more efficient way to calculate this with DAX in Power BI?
EDIT: This expression will work with the data presented above (minor edits to the accepted answer).
Expanded_Incidents =
GENERATE(
incidents;
SELECTCOLUMNS(
GENERATESERIES(
DATE(
YEAR(incidents[start_datetime]);
MONTH(incidents[start_datetime]);
DAY(incidents[start_datetime])
) + TIME(HOUR(incidents[start_datetime]);0;0);
incidents[end_datetime];
TIME(1;0;0)
);
"Expanded_incident_time"; [Value]
)
)
A more effectiv way would be to expand the incidents table so that each incident occurs for each time between the start and end datetime. This you can do with a calculated table like this:
Expanded_Incidents =
GENERATE(
incidents;
SELECTCOLUMNS(
GENERATESERIES(
incidents[start_date];
incidents[end_date];
TIME(h;m;s)
);
"Expanded_incident_time"; [Value]
)
)
Change the TIME(h;m;s) to the time resolution of your choosing.
Then make a relationship between your dateTime dim table and the [Expanded_incident_time] column (this should be a 1:*). Then you only have to make a measure which counts the number of rows in the new table. This will give you the number of active incidents during a specific time and I believe it will be faster than reitterating over the table each time.