我有一个棘手的问题,也许其中一个可以帮助您。
我有一列包含几个不同的值,其中一个是“不可用”。我想通过将那些“ Not Available”值替换为4个新值(例如“ Value 1”,“ Value 2”等)来创建一个计算列。平均分配,即25%将是“ Value 1”,25%将成为“价值2”等。我非常感谢我能获得的任何帮助。我应该提到我的数据存在于SSAS TDM中,所以我不能使用查询编辑器。
谢谢!
下面您会发现一些测试数据以及我想要的结果。
Example =
DATATABLE (
"Value", STRING,
{
{ "Not Available" },
{ "Not Available" },
{ "Not Available" },
{ "Not Available" },
{ "Not Available" },
{ "Not Available" },
{ "Not Available" },
{ "Not Available" },
{ "Not Available" },
{ "Not Available" },
{ "Not Available" },
{ "Not Available" },
{ "Bike" },
{ "Bike" },
{ "Bike" },
{ "Bike" },
{ "Skateboard" },
{ "Skateboard" },
{ "Skateboard" },
{ "Skateboard" },
{ "Skateboard" },
{ "Skateboard" },
{ "Snowboard" },
{ "Snowboard" },
{ "Snowboard" },
{ "Snowboard" },
{ "Snowboard" },
{ "Snowboard" },
{ "Penny Board" },
{ "Penny Board" },
{ "Penny Board" },
{ "BMX" },
{ "BMX" },
{ "BMX" }
}
)
所需结果:
答案 0 :(得分:2)
与其他答案一样,我假设您有一个索引替换表。我还将假设您可以为Example
表创建唯一索引(这是一个单独的问题)。
逻辑是一样的,但现在是DAX计算列:
Value2 =
IF (
Example[Value] = "Not Available",
LOOKUPVALUE (
'Replace'[Value],
'Replace'[Index], MOD ( Example[Index], COUNTROWS ( 'Replace' ) )
),
Example[Value]
)
答案 1 :(得分:0)
我宁愿在查询编辑器中执行此操作,因为在此创建索引列更容易。
确保两个表都已装入查询编辑器(替换表Replace
)中。
let
Source = Table.FromRows({{"Value1"},{"Value2"},{"Value3"},{"Value4"}},{"Value"}),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Value", type text}}),
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1)
in
#"Added Index"
还有您的示例表。
Replace
(在这种情况下为4)进行取模来转换索引列。Replace
合并到索引上匹配的Example
上。Value
列扩展为Value.1
。这是查询的M代码:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8ssvUXAsS8zMSUzKSVWK1RkuIk6Z2QQZwdmJJalJ+YlFKVTn5uWXU5sXkJqXV6ngRDTfyTcCk44FAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Value = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Value", type text}}),
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1),
#"Calculated Modulo" = Table.TransformColumns(#"Added Index", {{"Index", each Number.Mod(_, Table.RowCount(Replace)), type number}}),
#"Merged Queries" = Table.NestedJoin(#"Calculated Modulo",{"Index"},Replace,{"Index"},"Replace",JoinKind.LeftOuter),
#"Expanded Replace" = Table.ExpandTableColumn(#"Merged Queries", "Replace", {"Value"}, {"Value.1"}),
#"Added Custom" = Table.AddColumn(#"Expanded Replace", "Value2", each if [Value] = "Not Available" then [Value.1] else [Value], type text),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index", "Value.1"})
in
#"Removed Columns"