Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
evvx234
New Member

Finding the new value of a column by year

Hey everyone, 

 

I have a rather simple task I want implement in PowerBI. I am new to PowerBI and I am struggling with fitting the data types together. I have three columns of IDs, years and sales type (lets say fuel for the example). I want transform the data such that it has only the new type that the ID has bought that year. 


ID Type year
FuelProd_1 Gasoline 2020
FuelProd_1 Gasoline 2021
FuelProd_1 Diesel 2020
FuelProd_1 Diesel 2021
FuelProd_1 Diesel 2022
FuelProd_1 Hydrogen 2022
FuelProd_1 Hydrogen 2022
FuelProd_2 Gasoline 2020
FuelProd_2 Gasoline 2020
FuelProd_2 Gasoline 2020
FuelProd_2 Gasoline 2020
FuelProd_2 Gasoline 2022
FuelProd_2 Hydrogen 2022
FuelProd_2 Gasoline 2022


Becomes

 

ID Type year
FuelProd_1 Gasoline 2020
FuelProd_1 Diesel 2020
FuelProd_1 Hydrogen 2022
FuelProd_2 Gasoline 2020
FuelProd_2 Hydrogen 2022

 

I have the pieces. I would run through the dataset by ID and by year, find the unique type value and merge all the cleaned sets back together. I can filter on ID and on Year and use the distinct function but the data types (tables and columns) don't fit together. Also if I create a new table for each ID I would have way to much tables. 

There ought to be a better way to handle this task for large new of ID's and Types. I'm not very familiar with the DAX language and it doesn't exactly function like other languages I am more used to. 

 

Thanks in advance 🙂 

1 ACCEPTED SOLUTION
v-kongfanf-msft
Community Support
Community Support

Hi @evvx234 ,

 

You can try formula like below:

New_ =
VAR NewTable =
    SUMMARIZE ( 'Table', 'Table'[ID], 'Table'[Type] )
VAR Result =
    ADDCOLUMNS (
        NewTable,
        "Year",
            MINX (
                FILTER (
                    ALL ( 'Table' ),
                    'Table'[ID] = EARLIER ( 'Table'[ID] )
                        && 'Table'[Type] = EARLIER ( 'Table'[Type] )
                ),
                [Year]
            )
    )
RETURN
    Result

vkongfanfmsft_0-1706064005433.png

 

Best Regards,
Adamk Kong

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

 

View solution in original post

4 REPLIES 4
v-kongfanf-msft
Community Support
Community Support

Hi @evvx234 ,

 

You can try formula like below:

New_ =
VAR NewTable =
    SUMMARIZE ( 'Table', 'Table'[ID], 'Table'[Type] )
VAR Result =
    ADDCOLUMNS (
        NewTable,
        "Year",
            MINX (
                FILTER (
                    ALL ( 'Table' ),
                    'Table'[ID] = EARLIER ( 'Table'[ID] )
                        && 'Table'[Type] = EARLIER ( 'Table'[Type] )
                ),
                [Year]
            )
    )
RETURN
    Result

vkongfanfmsft_0-1706064005433.png

 

Best Regards,
Adamk Kong

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

 

LokambaTH
Helper I
Helper I

Might be due to the presence of multiple values in the Type column for some combinations of ID and Year.

 

NewColumn =
CALCULATE (
CONCATENATEX (
VALUES ( Table ),
Table[Type],
","
),
ALLEXCEPT ( Table, Table[ID], Table[Year] )
)

LokambaTH
Helper I
Helper I

Hi, Use a similar logic to get the unique Type for each ID and Year.

NewColumn =
CALCULATE(
VALUES(Table[Type]),
ALLEXCEPT(Table, Table[ID], Table[Year])
)

It gives an error: 'A table with multiple values ​​was specified when a single value was expected.'

I think because the new column doesn't have the dimensions of the original table. 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.