Excel 来自数据模型的数据透视图中的总计

Excel Grand Total in PivotChart from Data Model

目标

在要在 Excel 2016 年的数据透视图中显示的数据透视表中生成总计列(在下面称为 All)。

当前数据透视表

Sum of DailySales Column Labels
Row Labels North South
1/2/2021 1000 2000
1/3/2021 1500 2500

需要数据透视表

Sum of DailySales Column Labels
Row Labels North South All
1/2/2021 1000 2000 3000
1/3/2021 1500 2500 4000

数据洞察

所有数据都在数据模型中,并通过 StoreID 建立关系。数据来自两个表:

  1. 地区
    • 商店ID
    • (北部或南部)
  2. 销售额
    • 商店ID
    • 日期戳
    • 每日销售额

尝试次数

我讨厌的解决方案 我想出如何做到这一点的唯一方法是创建两个数据透视表,然后通过向导将它们组合成第三个。这会在我的文档中产生很大的开销,并且会导致更新变得混乱。

总结 任何帮助表示赞赏。至少感谢你一直坚持到现在。

给出如下数据样本:

您可以使用代码在 Power 查询中创建 table:

let

//read in the data
    Source = Excel.CurrentWorkbook(){[Name="Districts"]}[Content],
    districts = Table.TransformColumnTypes(Source,{{"StoreID", type text}, {"District", type text}}),
    Source2 = Excel.CurrentWorkbook(){[Name="Sales"]}[Content],
    sales = Table.TransformColumnTypes(Source2,
        {{"StoreID", type text},{"DateStamp", type date},{"DailySales", Int64.Type}}),

//Join the two tables
    joined = Table.NestedJoin(districts, "StoreID", sales,"StoreID","joined",JoinKind.FullOuter),

//expand the joined table
//then groupby date
    #"Expanded joined" = Table.ExpandTableColumn(joined, "joined", {"DateStamp", "DailySales"}, {"DateStamp", "DailySales"}),
    #"Grouped Rows" = Table.Group(#"Expanded joined", {"DateStamp", "District"}, {{"Values", each List.Sum([DailySales]), type nullable number}}),

//Pivot by District
    #"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[District]), "District", "Values", List.Sum),

//Add a column summing the districts for each date
    #"Added Custom" = Table.AddColumn(#"Pivoted Column", "All", each List.Sum({[North],[South]}))
in
    #"Added Custom"

看起来像:

然后加载到一个数据透视图,并创建图表: