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

How to combine SUMX and LOOKUPVALUE formulas? Getting sytax errors...

Hi there,

 

I have the following situation, and I couldn't find a solution to my problem.

 

I have two tables:

 

table A

raphazzz_0-1670604285137.png

 

 

and table B

raphazzz_1-1670604338026.png

 


* The values in the columns from jan to dec were input manually just for reference

 

Which formula or how do I return the sum of the values from column qty on table A to table B based on column state?

 

I tried this formula below, but I'm getting syntax errors.

jan = SUMX(
CALCULATETABLE(
'table A',
'table A'[month] = "jan",
'table A'[state] = LOOKUPVALUE( 'table A'[state], 'table A'[qty], table B[jan])
),
''table A'[qty]
)

 

Thanks!

Raphazzz

1 ACCEPTED SOLUTION
jgeddes
Super User
Super User

Here is a DAX formula that you can use to create a calculated table that will resemble Table B

stateTableLayout2 = 
SUMMARIZE(
    stateTable,
    stateTable[state],
    "Jan", CALCULATE(SUM(stateTable[qty]), ALLEXCEPT(stateTable, stateTable[state]), stateTable[month] = "jan"),
    "Feb", CALCULATE(SUM(stateTable[qty]), ALLEXCEPT(stateTable, stateTable[state]), stateTable[month] = "feb"),
    "Mar", CALCULATE(SUM(stateTable[qty]), ALLEXCEPT(stateTable, stateTable[state]), stateTable[month] = "mar"),
    "Apr", CALCULATE(SUM(stateTable[qty]), ALLEXCEPT(stateTable, stateTable[state]), stateTable[month] = "apr")
)

You would just have to keep adding rows in for the remaining months.
The end result looks like...

jgeddes_0-1670609455644.png

Of course you can do the same thing "easier" in Power Query by creating a new query referenced from your Table A and then Pivoting the [month] column and selecting Sum as the aggregation.




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





View solution in original post

4 REPLIES 4
Ashish_Mathur
Super User
Super User

Hi,

Create a matrix visual and drag this measure

Total = sum(Data[Qty])

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
v-yangliu-msft
Community Support
Community Support

Hi  @raphazzz ,

 

 

Here are the steps you can follow:

1. Go into Power Query – Copy TableA to form TableB.

vyangliumsft_0-1670813113083.png

2. TableB – Select Column [month] -- Transform – Pivot Column.

vyangliumsft_1-1670813113087.png

3. In the Pivot Column interface – select the column [qty].

vyangliumsft_2-1670813113087.png

4. Result:

vyangliumsft_3-1670813113088.png

 

Best Regards,

Liu Yang

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

Mikelytics
Resident Rockstar
Resident Rockstar

HI @raphazzz 

 

before you do anything you should bring table b in a normalized structure using the unpivot column feature in Power Query:

Unpivot columns - Power Query | Microsoft Learn

 

Afterwards you should have two table of the same kind and you can combine them in Power Query or with a relation and more.

 

Before:

Mikelytics_1-1670617496475.png

 

unpivoting

Mikelytics_2-1670617542169.png

 

after

Mikelytics_3-1670617557003.png

 

 

Best regards

Michael

-----------------------------------------------------

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

Appreciate your thumbs up!

@ me in replies or I'll lose your thread.

-----------------------------------------------------

LinkedIn

 

 

 

 

 

------------------------------------------------------------------
Visit my blog datenhungrig which I recently started with content about business intelligence and Power BI in German and English or follow me on LinkedIn!
jgeddes
Super User
Super User

Here is a DAX formula that you can use to create a calculated table that will resemble Table B

stateTableLayout2 = 
SUMMARIZE(
    stateTable,
    stateTable[state],
    "Jan", CALCULATE(SUM(stateTable[qty]), ALLEXCEPT(stateTable, stateTable[state]), stateTable[month] = "jan"),
    "Feb", CALCULATE(SUM(stateTable[qty]), ALLEXCEPT(stateTable, stateTable[state]), stateTable[month] = "feb"),
    "Mar", CALCULATE(SUM(stateTable[qty]), ALLEXCEPT(stateTable, stateTable[state]), stateTable[month] = "mar"),
    "Apr", CALCULATE(SUM(stateTable[qty]), ALLEXCEPT(stateTable, stateTable[state]), stateTable[month] = "apr")
)

You would just have to keep adding rows in for the remaining months.
The end result looks like...

jgeddes_0-1670609455644.png

Of course you can do the same thing "easier" in Power Query by creating a new query referenced from your Table A and then Pivoting the [month] column and selecting Sum as the aggregation.




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





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.