Home » excel » XLSXWriter and Excel "=FILTER()" Function?

XLSXWriter and Excel "=FILTER()" Function?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m successfully creating many Excel workbooks using XLSXWriter. Now I’m trying to place one of Excel’s new (as of 2019) FILTER functions into a cell:

=FILTER(A19:B90,B19:B90=E19)

When I open the workbook, Excel gives me this error dialog:

enter image description here

The workbook opens, but a “0” is in the cell instead of the FILTER function.

But if I paste the exact same filter function into the same cell manually, it works!

All the other formulas I’m creating work as expected, and if I use XLSXWriter to place a generic function into the same cell where I want the filter to go, e.g. =100 *5, it also works.

Does XLSXWriter have a bug when it comes to using =FILTER() functions?

How to&Answers:

This is a little bit of an odd one. From the XlsxWriter docs on Formulas added in Excel 2010 and later:

Excel 2010 and later added functions which weren’t defined in the original file specification. These functions are referred to by Microsoft as future functions. Examples of these functions are ACOT, CHISQ.DIST.RT , CONFIDENCE.NORM, STDEV.P, STDEV.S and WORKDAY.INTL.

When written using write_formula() these functions need to be fully qualified with a _xlfn. (or other) prefix as they are shown the list below. For example:

worksheet.write_formula('A1', '=_xlfn.STDEV.S(B1:B10)')

They will appear without the prefix in Excel

However, this formula has a _xlfn._xlws. prefix and is also an array formula so you would have to do this:

import xlsxwriter

workbook = xlsxwriter.Workbook('test.xlsx')
worksheet = workbook.add_worksheet()

worksheet.write_array_formula('C1:D72', 
                              '=_xlfn._xlws.FILTER(A19:B90,B19:B90=E19)')

workbook.close()

Output:

**enter image description here**

One difference between the XlsxWriter output and Excel’s is that this shows the array formula as {FILTER(...)}, with braces that are typical of array formulas, but Excel doesn’t. However, I think the formula works as intended. You can try it in a more complex example to verify.