[英]how to merge data from several tables in a pivot table
I want to create an overview monthly report for all the data in the worksheet.我想为工作表中的所有数据创建一个概览月度报告。 So for June, I'll see the sum of expenses, and sum of revenue, each existing in different tables.因此,对于 6 月份,我将看到费用总和和收入总和,每个都存在于不同的表格中。
Expense费用 | Date日期 | Price价格 |
---|---|---|
expense 1费用 1 | 22/06/2022 22/06/2022 | 100 100 |
expense 2费用 2 | 25/06/2022 25/06/2022 | 120 120 |
expense 3费用 3 | 07/07/2022 2022 年 7 月 7 日 | 60 60 |
Revenue收入 | Date日期 | Amount数量 |
---|---|---|
Salary薪水 | 11/06/2022 2022 年 11 月 6 日 | 150 150 |
Interest兴趣 | 01/07/2022 2022 年 1 月 7 日 | 20 20 |
Desired result期望的结果
Month月 | Expenses花费 | Revenue收入 |
---|---|---|
06-2022 06-2022 | 220 220 | 150 150 |
07-2022 07-2022 | 60 60 | 20 20 |
(I have a helper column month which converts date to a string month in the m-yyyy
format) (我有一个帮助列月份,它将日期转换为m-yyyy
格式的字符串月份)
I added all the tables to a data model, and created a pivot table from the data model.我将所有表添加到数据 model 中,并从数据 model 创建了 pivot 表。 My problem, is that even though each table has the hidden "month" field, eg 06-2022
, for a "row" I have to select a month field from a specific table, and then other table does not break down for that month.我的问题是,即使每个表都有隐藏的“月份”字段,例如06-2022
,对于“行”,我必须 select 来自特定表的月份字段,然后其他表在该月不会分解. And if I select both month
fields for the pivottable row, I just get nested data.如果我 select 两个month
字段都用于数据透视行,我只会得到嵌套数据。
Well, I was able to solve it, but I am not sure my answer is ideal, so I'll accept other answers.好吧,我能够解决它,但我不确定我的答案是否理想,所以我会接受其他答案。
I used Power Query.我使用了电源查询。 I created queries from each of the source tables, removing most of the columns, and keeping only date and price.我从每个源表中创建了查询,删除了大部分列,只保留了日期和价格。 I renamed the price column "revenue" for both source tables, and I multiplied the price (now renamed revenue) of the expenses table by -1
, since its function is to decrease revenue.我将两个源表的价格列重命名为“收入”,并将费用表的价格(现在重命名为收入)乘以-1
,因为它的 function 是为了减少收入。
I then joined the two queries into a new query, removed the "day" part of the date by using the "begining of month transformation on the new query".然后我将这两个查询加入到一个新查询中,通过使用“新查询的月初转换”删除了日期的“日”部分。 Then I grouped all the rows by date, using the sum function on the revenue column.然后我使用收入列上的总和 function 按日期对所有行进行分组。 I saved the new query.我保存了新查询。
Then I created a relationships between the month of the query, and the month field of the expense
table.然后我创建了查询月份和expense
表的月份字段之间的关系。 I did the same for income
table.我对income
表做了同样的事情。
Finally, I was able to create a pivot table from the data model, with the month of the month query as rows, sum of expense price column, sum of income amount column, and sum of (but it's only one value anyway) month's query revenue column.最后,我能够从数据 model 中创建一个 pivot 表,其中月份查询为行,费用价格列的总和,收入金额列的总和,以及(但无论如何它只是一个值)月份查询的总和收入栏。
This works, but is slow, needs manual refreshing, and causes "foreign data" alerts in excel.这可行,但速度很慢,需要手动刷新,并在 excel 中导致“外部数据”警报。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.