Forum Discussion
Create table with sum of columns
I have two tables, and each one shows sales of a store by year, month and week
| store | year | month | week | sales |
| A | 2021 | 1 | 1 | 2500 |
| A | 2021 | 1 | 2 | 1000 |
| ... | ... | ... | ... | ... |
| A | 2021 | 12 | 53 | 2000 |
| store | year | month | week | sales |
| B | 2021 | 1 | 1 | 500 |
| B | 2021 | 1 | 2 | 100 |
| ... | ... | ... | ... | ... |
| B | 2021 | 12 | 53 | 800 |
And I need to have a table with the same date columns but that has the sum of each sales.
| year | month | week | value |
| 2021 | 1 | 1 | 3000 |
| 2021 | 1 | 2 | 1100 |
| ... | ... | ... | ... |
| 2021 | 12 | 53 | 2800 |
I tried to have a measure but for the sales of a store it gave me the total, and I think I will need to save the data, so the table suits me, how can I create it?
nicolasvc
If you can append the two tables in Power Query, then you will have a single table. Add all the fields including the Sales column in a table visual except the Store , you should get the total as you need.
You can also create a new table in Power BI using the code below and do the same if you don't want to do it in Power Query.
New Table = UNION ( Table1 , Table2 )Hi,
Please check the below picture and the attached pbix file.
New Table =VAR newtablecalendar =SUMMARIZE ( TableA, TableA[Year], TableA[Month], TableA[Week of Year] )VAR add_sumcolumn =ADDCOLUMNS (newtablecalendar,"@total_value",CALCULATE (VAR currentyear =VALUES ( TableA[Year] )VAR currentmonth =VALUES ( TableA[Month] )VAR currentweek =VALUES ( TableA[Week of Year] )VAR slicertable =CROSSJOIN ( currentyear, currentmonth, currentweek )RETURNCALCULATE (SUMX ( TableA, TableA[Sales] ),TREATAS ( slicertable, TableA[Year], TableA[Month], TableA[Week of Year] ))+ CALCULATE (SUMX ( TableB, TableB[Sales] ),TREATAS ( slicertable, TableB[Year], TableB[Month], TableB[Week of Year] ))))RETURNadd_sumcolumn
2 Replies
- Fowmy
Super User
nicolasvc
If you can append the two tables in Power Query, then you will have a single table. Add all the fields including the Sales column in a table visual except the Store , you should get the total as you need.
You can also create a new table in Power BI using the code below and do the same if you don't want to do it in Power Query.
New Table = UNION ( Table1 , Table2 ) - Jihwan_Kim
Super User
Hi,
Please check the below picture and the attached pbix file.
New Table =VAR newtablecalendar =SUMMARIZE ( TableA, TableA[Year], TableA[Month], TableA[Week of Year] )VAR add_sumcolumn =ADDCOLUMNS (newtablecalendar,"@total_value",CALCULATE (VAR currentyear =VALUES ( TableA[Year] )VAR currentmonth =VALUES ( TableA[Month] )VAR currentweek =VALUES ( TableA[Week of Year] )VAR slicertable =CROSSJOIN ( currentyear, currentmonth, currentweek )RETURNCALCULATE (SUMX ( TableA, TableA[Sales] ),TREATAS ( slicertable, TableA[Year], TableA[Month], TableA[Week of Year] ))+ CALCULATE (SUMX ( TableB, TableB[Sales] ),TREATAS ( slicertable, TableB[Year], TableB[Month], TableB[Week of Year] ))))RETURNadd_sumcolumn