Forum Discussion
Need support with calculated columns
Hello together
A short note from my side to the proposed solution scenario II:
As I said unfortunately I have no idea how should looks the code of every calculated column in DAX to keep the same functionality as before unpivot.
But if my problem description was unclear I submit below how looks working solution for every calculated column #1, #2 and #3 in SQL.
I also attach the table after unpivot with some sample data under following link: https://drive.google.com/file/d/1oyfUhBwf_hNNmqqEDe0tHTSg72LCWLRM/view?usp=share_link . The SQL output below is based also on this sample data table.
#1. MeasurementID:
WITH
cte AS (
SELECT DISTINCT DataTime,
SerialNumber
FROM ALL_Table_unpivot)
SELECT DataTime,
SerialNumber,
ROW_NUMBER() OVER(ORDER BY SerialNumber ASC, DataTime ASC) AS MeasurementID
FROM cte;
Result (calculated in SQL on partial data, that's why the MeasurementID is lower than on the print screen from original post, but the calculation method of it is the same) :
#2. MeasurementNumber:
WITH
cte AS (
SELECT DISTINCT DataTime,
SerialNumber
FROM ALL_Table_unpivot)
SELECT DataTime,
SerialNumber,
ROW_NUMBER() OVER(PARTITION BY SerialNumber ORDER BY DataTime ASC) AS MeasurementNumber
FROM cte;Result:
#3. LastMeasurement:
WITH
cte1 AS (
SELECT DISTINCT DataTime,
SerialNumber
FROM ALL_Table_unpivot),
cte2 AS (
SELECT MAX(DataTime) as Last_DateTime_Of_SerialNumber,
SerialNumber
FROM cte1
GROUP BY SerialNumber)
SELECT cte1.DataTime,
cte1.SerialNumber,
CASE
WHEN cte1.DataTime IN (cte2.Last_DateTime_Of_SerialNumber) THEN 'True'
ELSE 'False'
END AS LastMeasurement
FROM cte1
JOIN cte2 ON cte1.SerialNumber = cte2.SerialNumber;Result:
If you also consider that different solution is better applicable as the solution of this problem I would like to hear you proposal as well.
I am grateful to everyone for help in this topic.