PowerBI: Calculate column containing current firmware based on occational messages - powerbi

I have a table called MachineEvents containing data similar to the one seen above.
I'm trying to create a measured column in DAX containing the current firmware of the machine.
This will be used to filter only messages when the machines has had certain firmware.
I've tried searching around but i have trouble finding similar problems.

Assuming you want your Firmware column to return the latest value from field message_info where message_type = 1, based on event_time, then use this DAX code in your Calculated Column:
Firmware =
VAR LastFirmwareTime =
CALCULATE (
MAX ( MachineEvents[event_time] ),
FILTER (
ALLEXCEPT ( MachineEvents, MachineEvents[machine_id] ),
MachineEvents[message_type] = 1 && MachineEvents[event_time] <= EARLIER ( MachineEvents[event_time] )
)
)
RETURN
CALCULATE (
VALUES ( MachineEvents[message_info] ),
FILTER (
ALLEXCEPT ( MachineEvents, MachineEvents[machine_id] ),
MachineEvents[message_type] = 1 && MachineEvents[event_time] = LastFirmwareTime
)
)
See worked example PBIX file here: https://pwrbi.com/so_60372050/

Related

Calculate the total from 2 tables - Powerbi

So, I have two tables I need to combine without merging as I was told merging the tables does not auto update when posted so had to separate my tables. So, I have Fioptics and legacy tables where I pull RecordID, JobTypeID and CustTypeID individually below is how my code looks
legacy res install =
CALCULATE (
COUNT ( LEGACY[RecordID] ),
FILTER (
LEGACY,
LEGACY[JobTypeID] = 1
&& LEGACY[CustTypeID] = 1
&& LEGACY[prod_grouping] = "legacy"
)
)
and
fioptics res install =
CALCULATE (
COUNT ( FIOPTIC[RecordID] ),
FILTER (
FIOPTIC,
FIOPTIC[JobTypeID] = 1
&& FIOPTIC[CustTypeID] = 1
&& FIOPTIC[prod_grouping] = "fioptics"
)
)
how do I go about using a dax function to pull from both RecordID's, JobTypeID and CustTypeID and filter from my FIOPTICS AND LEGACY tables at the same time? It might be a simple answer but having a brain fart maybe overlooking a simple solution to this problem.
You can combine those columns in a calculated table
Combined =
UNION(
SELECTCOLUMNS(
LEGACY,
"RecordID", LEGACY[RecordID],
"JobTypeID", LEGACY[JobTypeID],
"CustTypeID", LEGACY[CustTypeID]
),
SELECTCOLUMNS(
FIOPTICS,
"RecordID", FIOPTICS[RecordID],
"JobTypeID", FIOPTICS[JobTypeID],
"CustTypeID", FIOPTICS[CustTypeID]
)
)
And of cause you can filter the combined table in your report to your needs.

PowerBI report cannot recognize If statement when string are compared

I'm trying to replicate the example presented in this youtube tutorial
https://www.youtube.com/watch?v=z9ttZAZkEhs
However, even if I use the same DAX code the controls do not recognize the values properly.
Selected = calculate (DimProduct[uniqueCustomer], treatas( Values(Products[Name]), DimProduct[EnglishProductName] ) )
I tried different ways to recognize the values coming from the slicer, but they simply do not work.
CheckColumn = if (trim(DimProduct[EnglishProductName]) = trim(DimProduct[SelectedNumber2]),true,false)
I have attached the example file that I'm using.
https://github.com/gabrielacosta/TestPowerBiSlicer/blob/main/testslicer.pbix
Does anyone know what could be the issue.
I guess your calculation should look like the formula below but it doesn't make much sense doing that calculation.
Selected =
CALCULATE (
[uniqueCustomer],
TREATAS ( VALUES ( Products[Name] ), DimProduct[EnglishProductName] ),
VALUES ( DimProduct[EnglishProductName] )
)
For this specific scenario just using SELECTEDVALUE is better
Selected2 =
VAR SelectedProduct =
SELECTEDVALUE ( Products[Name] )
VAR CountCalc =
CALCULATE (
[uniqueCustomer],
FILTER ( DimProduct, [EnglishProductName] = SelectedProduct )
)
RETURN
CountCalc

A circular dependency in Power Bi

I calculated a field that brings the total sales for bikes in 2018-all work fine
copy the same formula to another column to calculate the same for 2017 brought this reference error -how can I move around it if I want to show a few columns using the same formula with different parameters
Sales_Bike_2018 =
CALCULATE (
[Total_Sales],
TrainingSample2[Business Segment] = "Bikes",
TrainingSample2[Year] = 2018
)
Sales_Bike_2017 =
CALCULATE (
[Total_Sales],
TrainingSample2[Business Segment] = "Bikes",
TrainingSample2[Year] = 2017
)
The problem is that you are creating 2 columns on a table that doesn't have a Unique Key.
When you create Sales_Bike_2018 everything works fine because it depends on the rest of the columns of your table but when you create the column Sales_Bike_2017, the code of Sales_Bike_2017 depends on rest of the columns as well as on Sales_Bike_2018.
Similary if it was possible to create Sales_Bike_2017 then Sales_Bike_2018 would have depended upon Sales_Bike_2017 and that's not allowed and that's why you get a circular dependency error.
Solution:
Remove filters coming from both new columns due to context transition by using REMOVEFILTERS ()
Sales_Bike_2018 =
CALCULATE (
[Total_Sales],
TrainingSample2[Business Segment] = "Bikes",
TrainingSample2[Year] = 2018,
REMOVEFILTERS ( TrainingSample2[Sales_Bike_2017] )
)
Sales_Bike_2017 =
CALCULATE (
[Total_Sales],
TrainingSample2[Business Segment] = "Bikes",
TrainingSample2[Year] = 2017,
REMOVEFILTERS ( TrainingSample2[Sales_Bike_2018] )
)

DAX Count Max Value By Group

I have a survey that is sent to companies periodically and each new survey is given a collection #. I'm trying to create a column that tells me the maximum collection # for each company code. This will vary by company because not every company responds to every survey.
LastCollection = (FILTER('Dynamic', 'Dynamic'[Collection]
=CALCULATE(max('Dynamic'[Collection]),ALLEXCEPT('Dynamic','Dynamic'[Company Name])))
The error I'm getting is that the expression refers to multiple columns. I attempted to wrap it in AVERAGE but that didn't help.
You should just be able to do something along the lines of the below.
LastCollection =
VAR companyName = 'Dynamic'[Company Name]
RETURN CALCULATE( MAX ('Dynamic'[Collection] ),
FILTER ( 'Dynamic', 'Dynamic'[Company Name] = companyName ))
I'm not sure why you have the CALCULATE inside a FILTER since a FILTER returns a table rather than a single value.
This should be simpler:
LastCollection =
CALCULATE (
MAX ( 'Dynamic'[Collection] ),
ALLEXCEPT ( 'Dynamic', 'Dynamic'[Company Name] )
)

Power BI - Getting the most recent value from a related table

I know this must be extremely simple, but every example I can find online only works within a single table. I've simplified my situation to these two tables:
I want to add a calculated column to the first table, showing the most recent value for that id. It also needs to work with text.
There are a variety of ways to do this kind of thing as I've explained before and all of the solutions there can be adjusted to work in this case.
Doing this as a calculated column and with a second table, you need to make sure you are using row context and filter context appropriately.
Here's are a couple different possibilities I think may work:
MostRecentValue =
MAXX ( TOPN ( 1, RELATEDTABLE ( Table2 ), Table2[date] ), Table2[value] )
In this one, RELATEDTABLE is doing the work of filtering Table2 to only the rows where id matches Table1.
MostRecentValue =
VAR PrevDate = CALCULATE ( MAX ( Table2[date] ) )
RETURN CALCULATE ( MAX ( Table2[value] ), Table2[date] = PrevDate )
The relationship is more subtle here. Wrapping the MAX in CALCULATE forces a context transition so that the row context (which includes id) is applied to Table2 as filter context.