简体   繁体   English

将自定义字段添加到“数据透视表”中-Power Query

[英]Add custom field to a “pivot table” - Power Query

I've used Power Query to add custom fields to a table made from 2 merged tables in order to simulate a pivot table. 我已经使用Power Query将自定义字段添加到由2个合并表组成的表中,以模拟数据透视表。 However, I can't seem to add a filter to my final table. 但是,我似乎无法在最终表中添加过滤器。 Is there another way to do this? 还有另一种方法吗?

I've tried to use the Pivot table from Excel, but I can't seem to insert calculated field as desired. 我尝试使用Excel中的数据透视表,但似乎无法按需插入计算字段。

Here's my Excel file: https://ufile.io/x2v1j 这是我的Excel文件: https : //ufile.io/x2v1j

I'll start with a disclaimer that I'm not exactly sure I know what you're trying to do; 首先,我不确定我是否知道您要尝试的内容。 but I took a stab at this anyway. 但是无论如何我还是被刺了。

I figured you were trying to filter the months in the T_Catégories query, before your grouping; 我发现您在分组之前试图过滤T_Catégories查询中的月份 so I added a manual filter step there. 所以我在那里添加了手动过滤步骤。 When I did that and deselected months, your T_Final query broke. 当我这样做并取消选择月份时,您的T_Final查询中断了。 The reason is because, as I filtered out months, it also filtered out categories that your T_Final query relied upon for column names. 原因是因为在我筛选出数月之后,它还筛选出了T_Final查询中列名称所依赖的类别。 For instance, this affected your calculations that relied upon column names. 例如,这影响了您依赖列名称的计算。 I had to change your T_Final query so that it would dynamically determine the column names. 我必须更改您的T_Final查询,以便它可以动态确定列名。

Again, I'm not exactly sure about what you're trying to do, so I may have gotten it wrong with respect to the calculations, but this might help get you closer at least. 同样,我不确定您要做什么,因此我可能在计算方面弄错了,但这至少可以帮助您更加紧密。

Like I said, in T_Catégories , I added the filter: 就像我说的那样,在T_Catégories中 ,我添加了过滤器:

在此处输入图片说明

在此处输入图片说明

That's when things broke for T_Final . 那是T_Final崩溃的时候 So in T_Final , I needed to: 因此,在T_Final中 ,我需要:

Change the step Valeur remplacée1 to = Table.ReplaceValue(#"Colonne dynamique",null,0,Replacer.ReplaceValue,Table.ColumnNames(#"Colonne dynamique")) (I was pretty sure you were using the columns resulting from the previous step Colonne dynamique .) 将步骤Valeurremplacée1更改为= Table.ReplaceValue(#"Colonne dynamique",null,0,Replacer.ReplaceValue,Table.ColumnNames(#"Colonne dynamique")) (我很确定您使用的是前一列的结果步骤Colonne dynamique 。)

Change the step Personnalisée ajoutée3 to = Table.AddColumn(#"Valeur remplacée1", "Total général", each List.Sum(List.RemoveFirstN(Record.ToList(_),1))) (This is making a list from the record, then removing the first entry of the list and summing what remains in the list.) 将步骤Personnaliséeajoutée3更改= Table.AddColumn(#"Valeur remplacée1", "Total général", each List.Sum(List.RemoveFirstN(Record.ToList(_),1))) (这是从记录,然后删除列表的第一项并汇总列表中剩余的内容。)

Change the step Colonnes permutées to = Table.ReorderColumns(#"Personnalisée ajoutée3",Table.ColumnNames(#"Personnalisée ajoutée3")) (I was pretty sure you were using the column resulting from the previous step Personnalisée ajoutée3 .) 将步骤Colonnespermutées更改= Table.ReorderColumns(#"Personnalisée ajoutée3",Table.ColumnNames(#"Personnalisée ajoutée3")) (我很确定您正在使用上一步Personnaliséeajoutée3产生的列。)

Change the step Personnalisée ajoutée to = Table.AddColumn(#"Colonnes permutées", "Indisponibilté", each List.Sum(List.RemoveLastN(List.RemoveFirstN(Record.ToList(_),1),2))) (This is making a list from the record, then removing the first entry of the list, then removing the last two entries of the list, and summing what remains in the list. This is especially where I'm not sure I added the items you intended. At least you can see what I did to be able to add the columns without using static column names.) 将步骤Personnaliséeajoutée更改= Table.AddColumn(#"Colonnes permutées", "Indisponibilté", each List.Sum(List.RemoveLastN(List.RemoveFirstN(Record.ToList(_),1),2))) (此正在从记录中创建一个列表,然后删除列表的第一个条目,然后删除列表的最后两个条目,并对列表中剩余的内容进行求和。尤其是在我不确定我添加了您想要的项目的地方。至少您可以看到我在不使用静态列名的情况下添加列的操作。)

Here's the m code for the three queries: 这是三个查询的m代码:

T_Catégories: T_Catégories:

let
Source = Excel.CurrentWorkbook(){[Name="T_Catégories"]}[Content],
#"Type modifié" = Table.TransformColumnTypes(Source,{{"Métier", type text}, {"Code absence", Int64.Type}, {"Date", type date}, {"Catégorie", type text}}),
#"Colonnes supprimées" = Table.RemoveColumns(#"Type modifié",{"Code absence", "Date"}),
#"Filtered Rows" = Table.SelectRows(#"Colonnes supprimées", each true),
#"Lignes groupées" = Table.Group(#"Filtered Rows", {"Métier", "Catégorie"}, {{"Nombre", each Table.RowCount(_), type number}})
in
#"Lignes groupées"

T_métiers: T_métiers:

let
Source = Excel.CurrentWorkbook(){[Name="T_métiers"]}[Content],
#"Type modifié" = Table.TransformColumnTypes(Source,{{"Métier", type text}, {"Nombre", Int64.Type}})
in
#"Type modifié"

T_Final: T_Final:

let
Source = Table.Combine({T_Catégories, T_métiers}),
#"Valeur remplacée" = Table.ReplaceValue(Source,null,"Nombre employés",Replacer.ReplaceValue,{"Catégorie"}),
#"Colonne dynamique" = Table.Pivot(#"Valeur remplacée", List.Distinct(#"Valeur remplacée"[Catégorie]), "Catégorie", "Nombre"),
#"Valeur remplacée1" = Table.ReplaceValue(#"Colonne dynamique",null,0,Replacer.ReplaceValue,Table.ColumnNames(#"Colonne dynamique")),
#"Personnalisée ajoutée3" = Table.AddColumn(#"Valeur remplacée1", "Total général", each List.Sum(List.RemoveFirstN(Record.ToList(_),1))),
#"Colonnes permutées" = Table.ReorderColumns(#"Personnalisée ajoutée3",Table.ColumnNames(#"Personnalisée ajoutée3")),
#"Personnalisée ajoutée" = Table.AddColumn(#"Colonnes permutées", "Indisponibilté", each List.Sum(List.RemoveLastN(List.RemoveFirstN(Record.ToList(_),1),2))),
#"Personnalisée ajoutée1" = Table.AddColumn(#"Personnalisée ajoutée", "Disponibilté", each [Nombre employés]*7.5),
#"Personnalisée ajoutée2" = Table.AddColumn(#"Personnalisée ajoutée1", "Taux disponibilté (%)", each (1-[Indisponibilté]/[Disponibilté])*100),
#"Type modifié" = Table.TransformColumnTypes(#"Personnalisée ajoutée2",{{"Indisponibilté", Int64.Type}, {"Disponibilté", type number}, {"Taux disponibilté (%)", type number}})
in
#"Type modifié"

I would think you can progress from here fairly well. 我认为您可以从这里取得很好的进展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM