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
learner4eva
Helper I
Helper I

Use DAX to add rows to table and use average of existing data as value for new rows

How do I use DAX to create a new table with additional rows? The value in the new rows will be using average of the existing rows for each category (version). 

learner4eva_0-1652080036698.png

 

1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Hi,

Please check the below attached pbix file and the DAX formula for creating a new table.

I believe you have a Calendar Table, like the attached sample pbix file.

 

NewTable = 
UNION( Data, 
ADDCOLUMNS (
    SUMMARIZE (
        ADDCOLUMNS (
            ADDCOLUMNS (
                ADDCOLUMNS (
                    SUMMARIZE ( Data, Data[Version], Data[WeekNum] ),
                    "@rowcount", COUNTROWS ( FILTER ( Data, Data[Version] = EARLIER ( Data[Version] ) ) ),
                    "@weekoffsetnumber",
                        MAXX (
                            FILTER ( 'Calendar', 'Calendar'[WeekNumber] = Data[WeekNum] ),
                            'Calendar'[WeekOffset]
                        )
                ),
                "@newweekoffsetnumber", [@weekoffsetnumber] + [@rowcount]
            ),
            "@newweeknumber",
                MAXX (
                    FILTER ( 'Calendar', 'Calendar'[WeekOffset] = [@newweekoffsetnumber] ),
                    'Calendar'[WeekNumber]
                )
        ),
        Data[Version],
        [@newweeknumber]
    ),
    "@valueavg",
        AVERAGEX (
            FILTER ( Data, Data[Version] = EARLIER ( Data[Version] ) ),
            Data[Value]
        )
)
)

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


View solution in original post

4 REPLIES 4
Jihwan_Kim
Super User
Super User

Hi,

Please check the below attached pbix file and the DAX formula for creating a new table.

I believe you have a Calendar Table, like the attached sample pbix file.

 

NewTable = 
UNION( Data, 
ADDCOLUMNS (
    SUMMARIZE (
        ADDCOLUMNS (
            ADDCOLUMNS (
                ADDCOLUMNS (
                    SUMMARIZE ( Data, Data[Version], Data[WeekNum] ),
                    "@rowcount", COUNTROWS ( FILTER ( Data, Data[Version] = EARLIER ( Data[Version] ) ) ),
                    "@weekoffsetnumber",
                        MAXX (
                            FILTER ( 'Calendar', 'Calendar'[WeekNumber] = Data[WeekNum] ),
                            'Calendar'[WeekOffset]
                        )
                ),
                "@newweekoffsetnumber", [@weekoffsetnumber] + [@rowcount]
            ),
            "@newweeknumber",
                MAXX (
                    FILTER ( 'Calendar', 'Calendar'[WeekOffset] = [@newweekoffsetnumber] ),
                    'Calendar'[WeekNumber]
                )
        ),
        Data[Version],
        [@newweeknumber]
    ),
    "@valueavg",
        AVERAGEX (
            FILTER ( Data, Data[Version] = EARLIER ( Data[Version] ) ),
            Data[Value]
        )
)
)

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


Thank you Jihwan, this works very well!

amitchandak
Super User
Super User

@learner4eva ,

 

union(Table,
crossjoin(summarize(filter(Table, Table[Week] >=202140 && Table[Week] <=202144), [Week]), AvergaeX(summarize(filter(Table, Table[Week] >=202140 && Table[Week] <=202144), "_1", Average(Table[Value])),[_1])),

crossjoin(summarize(filter(Table, Table[Week] >=202145 && Table[Week] <=202145), [Week]), AvergaeX(summarize(filter(Table, Table[Week] >=202145 && Table[Week] <=202145), "_1", Average(Table[Value])),[_1]))
)

Hi Amit,

 

Thank you for the fast reply..

Problem is I have about 1000 versions (1st column).. the screenshot is only showing 2 out of 1000 versions..
I also have a calendar table that allows me to lookup the weeknum.

 

Can you advise? Thank you

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.