Excel – 基于两个条件返回值

huangapple go评论136阅读模式
英文:

Excel - Return value based in two conditions

问题

我需要获取两列之间的交叉值:
Excel – 基于两个条件返回值

我只能使用辅助列,是否可能在一个单元格公式中实现?

提前感谢。

英文:

I need to get the cross values beetween 2 columns:
Excel – 基于两个条件返回值

i only can with auxiliary column, is possible to get in one cell formula?

Thks in advance

答案1

得分: 1

尝试使用 XLOOKUP()FILTER() 函数。


• 单元格 C1 中使用的公式:

=XLOOKUP(A1,$E$2:$E$7,FILTER($F$2:$K$7,$F$1:$K$1=B1))

或者,如果你喜欢使用自动填充公式的方法,可以使用 MAP()


• 单元格 C1 中使用的公式:

=MAP(A1:A6,B1:B6,LAMBDA(x,y,XLOOKUP(x,E2:E7,FILTER(F2:K7,F1:K1=y))))

或者,使用简单的 VLOOKUP()MATCH() 函数。


• 单元格 C1 中使用的公式:

=VLOOKUP(A1,$E$2:$K$7,MATCH(B1,$E$1:$K$1,0),0)
英文:

Try using <kbd>XLOOKUP( )</kbd> with <kbd>FILTER( )</kbd> function

Excel – 基于两个条件返回值


• Formula used in cell C1

=XLOOKUP(A1,$E$2:$E$7,FILTER($F$2:$K$7,$F$1:$K$1=B1))

Or, if you like to Spill the formula then using <kbd>MAP( )</kbd>

Excel – 基于两个条件返回值


• Formula used in cell C1

=MAP(A1:A6,B1:B6,LAMBDA(x,y,XLOOKUP(x,E2:E7,FILTER(F2:K7,F1:K1=y))))

Or, simple <kbd>VLOOKUP( )</kbd> with <kbd>MATCH( )</kbd> function

Excel – 基于两个条件返回值


• Formula used in cell C1

=VLOOKUP(A1,$E$2:$K$7,MATCH(B1,$E$1:$K$1,0),0)

答案2

得分: 1

以下是翻译好的部分:

"Lots of great ways to do this, but I recommend using the UNPIVOT lambda here:
https://gist.github.com/ExcelRobot/54fe4bf04f24a83b83d3b3bddda4d3c3

Since SO answers prefer embedded content rather than links, I've included the function definitions below.

Don't be put off by the length of the function. You don't need to worry about how it works, although if you really want to understand it, you're welcome to read this article I wrote explaining it.

Install the Excel Labs Add-in so you can use the Advanced Formula Environment. The add-in is developed by Microsoft Research and is definitely trustworthy. After that's installed, you'll have the benefit of using it on any function or formula going forward. It's worth a few extra minutes.

To get the UNPIVOT function into your workbook, use the "Import from URL" button in Excel Labs:

Excel – 基于两个条件返回值

Paste the link into the dialog and click 'Import':

Excel – 基于两个条件返回值

After the function is imported, hit Save in Excel Labs:

Excel – 基于两个条件返回值

Now you can just enter this formula to unpivot your table:

=UNPIVOT(A1:G7,B1:G1)

Excel – 基于两个条件返回值
"

英文:

Lots of great ways to do this, but I recommend using the UNPIVOT lambda here:
https://gist.github.com/ExcelRobot/54fe4bf04f24a83b83d3b3bddda4d3c3

Since SO answers prefer embedded content rather than links, I've included the function definitions below.

Don't be put off by the length of the function. You don't need to worry about how it works, although if you really want to understand it, you're welcome to read this article I wrote explaining it.

Install the Excel Labs Add-in so you can use the Advanced Formula Environment. The add-in is developed by Microsoft Research and is definitely trustworthy. After that's installed, you'll have the benefit of using it on any function or formula going forward. It's worth a few extra minutes.

To get the UNPIVOT function into your workbook, use the "Import from URL" button in Excel Labs:

Excel – 基于两个条件返回值

Paste the link into the dialog and click 'Import':

Excel – 基于两个条件返回值

After the function is imported, hit Save in Excel Labs:

Excel – 基于两个条件返回值

Now you can just enter this formula to unpivot your table:

=UNPIVOT(A1:G7,B1:G1)

Excel – 基于两个条件返回值

/*
Name: Unpivot Table (UNPIVOT)
Description: Given a table range with headers and array of header names, unpivots the 
   specified columns in place, optionally removing any blank entries.
Written By: Excel Robot (@ExcelRobot)
Category: Array
*/
UNPIVOT = LAMBDA(table, columns_to_unpivot, [attribute_name], [value_name], [remove_blanks], LET(
   _AttributeLabel, IF(ISOMITTED(attribute_name), &quot;Attribute&quot;, attribute_name),
   _ValueLabel, IF(ISOMITTED(value_name), &quot;Value&quot;, value_name),
   _FirstColumnToUnpivot, MATCH(INDEX(columns_to_unpivot, , 1), INDEX(table, 1, ), 0),
   _UnpivotColumnCount, COLUMNS(columns_to_unpivot),
   _ColumnNumbers, SEQUENCE(1, COLUMNS(table)),
   _IncludeColumns, (_ColumnNumbers &gt;= _FirstColumnToUnpivot) * (_ColumnNumbers &lt; _FirstColumnToUnpivot + _UnpivotColumnCount),
   _UnpivotColumns, FILTER(_ColumnNumbers, _IncludeColumns),
   _OtherColumns, FILTER(_ColumnNumbers, NOT(_IncludeColumns)),
   _FullOuterJoin, CROSSJOIN(CHOOSECOLS(table, _OtherColumns), VSTACK(_AttributeLabel, TRANSPOSE(columns_to_unpivot)),TRUE),
   _WithValues, HSTACK(_FullOuterJoin, VSTACK(_ValueLabel, TOCOL(DROP(CHOOSECOLS(table, _UnpivotColumns), 1)))),
   _RemoveBlanks, IF(OR(ISOMITTED(remove_blanks), remove_blanks), FILTER(_WithValues, INDEX(_WithValues, , COLUMNS(_WithValues)) &lt;&gt; &quot;&quot;), IF(_WithValues = &quot;&quot;, &quot;&quot;, _WithValues)),
   _ColumnOrder, LET(
      n, COLUMNS(_RemoveBlanks),
      s, SEQUENCE(1, n),
      IFS(
         s &lt; _FirstColumnToUnpivot, s,
         s &lt; _FirstColumnToUnpivot + 2, s + n - _FirstColumnToUnpivot - 1,
         TRUE, s - 2
      )
   ),
   _ReorderColumns, CHOOSECOLS(_RemoveBlanks, _ColumnOrder),
   _ReorderColumns
));

/*
Name: Cross Join Tables (CROSSJOIN)
Description: Given two tables, returns the Cartesian product of rows from the tables.
Written By: Excel Robot (@ExcelRobot)
Category: Array
*/
CROSSJOIN = LAMBDA(table1, table2, [has_headers], LET(
   _HasHeaders, IF(ISOMITTED(has_headers), TRUE, has_headers),
   _Data1, IF(_HasHeaders, DROP(table1,1), table1),
   _Data2, IF(_HasHeaders, DROP(table2,1), table2),
   _D1Rows, ROWS(_Data1),
   _D1Cols, COLUMNS(_Data1),
   _D2Rows, ROWS(_Data2),
   _D2Cols, COLUMNS(_Data2),
   _OuterJoinedData, MAKEARRAY(_D1Rows * _D2Rows, _D1Cols + _D2Cols,LAMBDA(i, j,
      IF(j &lt;= _D1Cols, INDEX(_Data1, ROUNDUP(i / _D2Rows, 0), j), INDEX(_Data2, MOD(i - 1, _D2Rows) + 1, j - _D1Cols)))),
   _WithHeader, IF(_HasHeaders, VSTACK(HSTACK(TAKE(table1, 1), TAKE(table2, 1)), _OuterJoinedData), _OuterJoinedData),
   _WithHeader
));

答案3

得分: 0

公式是:=INDEX($E$1:$K$7;MATCH(A1;$E$1:$E$7;0);MATCH(B1;$E$1:$K$1;0))

请注意,只有在用于获取交叉值的列中没有重复值时,此公式才有效。

英文:

Excel – 基于两个条件返回值

Formula is: =INDEX($E$1:$K$7;MATCH(A1;$E$1:$E$7;0);MATCH(B1;$E$1:$K$1;0))

Notice this will work only if columns used to get the crossed value got no duplicates

答案4

得分: 0

你还可以使用Offset()函数,从$D$1开始,向右和向下移动相应的单元格数量:

=OFFSET($D$1,MATCH(A1,$D$2:$D$7,0),MATCH(B1,$E$1:$J$1,0))

解释:

  • MATCH(A1,$D$2:$D$7,0): 在字母列表中可以找到"A1"的位置是哪里?(这将成为从"$D$1"向下移动的行数)
  • MATCH(B1,$E$1:$J$1,0): 在数字列表中可以找到"B1"的位置是哪里?(这将成为从"$D$1"向右移动的列数)
英文:

You can also use the Offset() function, starting from $D$1, and going the right amount of cells to the right and to the bottom:

=OFFSET($D$1,MATCH(A1,$D$2:$D$7,0),MATCH(B1,$E$1:$J$1,0))

Explanation:

  • MATCH(A1,$D$2:$D$7,0): at which place can you find "A1" in the list of letters? (This becomes the number of rows to go down from "$D$1")
  • MATCH(B1,$E$1:$J$1,0): at which place can you find "B1" in the list of digits? (This becomes the number of columns to go to the right from "$D$1")

答案5

得分: 0

The INDEX-function is just about tailor-made for this.
INDEX函数几乎是为此定制的。
Your horizontal array seem to be an index already, and if that holds for the actual data you can simply use this one.
您的水平数组似乎已经是一个索引,如果对实际数据也成立,您可以简单地使用这个。

Excel – 基于两个条件返回值

However if the horizontal array is not a sequence of 1, 2, 3 and so then use MATCH for both arrays, like this:
但是,如果水平数组不是1、2、3等序列,那么请对两个数组使用MATCH函数,如下所示:

Excel – 基于两个条件返回值

I didn't use "$" in the example for better readability, but you want to apply that for the formula to function to work when you copy it downwards.
为了更好地可读性,我在示例中没有使用“$”,但是当您向下复制它时,您希望应用该符号,以使公式正常工作。

英文:

The INDEX-function is just about tailor-made for this.
Your horizontal array seem to be an index already, and if that holds for the actual data you can simply use this one.

Excel – 基于两个条件返回值

However if the horizontal array is not a sequence of 1, 2, 3 and so then use MATCH for both arrays, like this:

Excel – 基于两个条件返回值

I didn't use "$" in the example for better readability, but you want to apply that for the formula to function to work when you copy it downwards.

答案6

得分: 0

双重 FILTER 也可以完成任务:

=FILTER(FILTER($E$2:$J$7,$D$2:$D$7=A1),$E$1:$J$1=B1)

结果:

Excel – 基于两个条件返回值

英文:

Double FILTER also could do the job:

=FILTER(FILTER($E$2:$J$7,$D$2:$D$7=A1),$E$1:$J$1=B1)

Result:

Excel – 基于两个条件返回值

huangapple
  • 本文由 发表于 2023年7月13日 19:51:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76679069.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定