小编Log*_*ter的帖子

Powershell Group by Multiple Properties

我试图确定是否有一种更简单的方法来编写一个Powershell函数,该函数通过多个属性对数组进行分组并对组中的指定属性求和,类似于以下内容:

#Ungrouped data
ID ID2 Value
-- --- -----
A  A1    100
A  A2    200
A  A2    300
B  B1    400
B  B1    500
B  B1    600
B  B3    700
C  C1    800
C  C2    900
C  C1   1000

#Grouped data
ID ID2 Sum Value
-- --- ---------
A  A1        100
A  A2        500
B  B1       1500
B  B3        700
C  C1       1800
C  C2        900
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止所拥有的:

$grouped = $ungrouped | group ID, ID2
$grouped | foreach {
    [pscustomobject] @{ …
Run Code Online (Sandbox Code Playgroud)

powershell

6
推荐指数
1
解决办法
8906
查看次数

Power Query根据另一列转换列

我一直认为这应该很容易,但答案是回避我.在Excel Power Query中,我想根据另一列的值转换列的每一行中的值.例如,假设我有Table1如下:

Column A | Column B
-------------------
X        | 1
Y        | 2
Run Code Online (Sandbox Code Playgroud)

我想根据列B中的值转换A列中的值,而不必添加新列并替换原始列A.我尝试使用TransformColumns但输入只能是目标列的值 - 我可以从TransformColumns函数中访问行/记录中的其他字段值.我能够做这样的事情:

=Table.TransformColumns(Table1, {"Column A", each if [Column B]=1 then "Z" else _ })
Run Code Online (Sandbox Code Playgroud)

这将导致:

Column A | Column B
-------------------
Z        | 1
Y        | 2
Run Code Online (Sandbox Code Playgroud)

我知道有办法做到这一点,但我试图找到一个步骤/转换量最少的方法.例如,我知道我可以使用Table.AddColumn 基于查看B列的函数添加新的 A列,但是我必须删除原始的A列并将其替换为新的A列,这需要多个额外的步骤.

excel powerquery

6
推荐指数
2
解决办法
1万
查看次数

使用Set-Item和GetNewClosure创建Powershell函数

我正在尝试构建一个函数,它本身可以通过Set-Item命令创建函数,我将新函数的scriptblock传递给Set-Item的-Value参数.我遇到一个问题,在脚本块上使用GetNewClosure似乎不起作用,我只是不知道我做错了什么.

在下面的代码中,首先我手动创建一个函数(testFunc)按预期工作,在函数创建后将$ x设置为2将不会导致函数返回2; 相反,它返回1,因为这是创建函数时$ x的值.但是当我尝试通过make-function函数执行相同操作时,行为会发生变化.

我敢肯定我忽视了一些小事.

> $x = 1
> $block = {$x}.GetNewClosure()
> Set-Item function:global:testFunc -Value $block
> testFunc
1 

> $x = 2
> testFunc
1 # still 1 because of GetNewClosure - this is expected behavior

> $x = 1
> function make-function { $block2 = {$x}.GetNewClosure()
       Set-Item function:global:testFunc2 -Value $block2
  }
> make-function
> testFunc2
1 

> $x = 2
> testFunc2
2 # Why is it not returning 1 in this case? 
Run Code Online (Sandbox Code Playgroud)

powershell scriptblock

4
推荐指数
1
解决办法
495
查看次数

标签 统计

powershell ×2

excel ×1

powerquery ×1

scriptblock ×1