Connect two pivot table in libreoffice calc

Well, an example task should be clarified by the OP.

Off topic: To my surprise I found something that Excel can do since 8 years but nobody ever asks to do this in Calc:
https://www.pcworld.com/article/440704/how-to-create-relational-databases-in-excel-2013.html

People simply don’t get the concept of a relational database, even if it is an Excel feature.

this is not my answer.

The Slicer object is supported since Excel 2010.
You can support the enhancement request #tdf119807 by Roman Kuznetsov.

@kesu, let’s say I created two pivot tables based on the same data source and applied a standard filter to one table and programmatically synchronized the second with the first one. BUT. I am unable to add an event listener to the first table in order to automatically invoke the sync routine on the second table.

Who knows how to intercept the data filtering event in a pivot table 1?

Sub SyncDataPilotsByFilter()
'''	Synchronize DataPilot1 with DataPilot2 data by filter using the filter set in DataPilot1.
	On Local Error GoTo HandleErrors
	Dim oSheet As Object, oTables As Object, oTable1 As Object, oTable2 As Object

	oSheet = ThisComponent.Sheets.getByName("PivotTables")
	oTables = oSheet.DataPilotTables
	' Define table names if necessary.
	'oTables(0).Name = "DataPilot1"
	'oTables(1).Name = "DataPilot2"
Rem	Xray oTables: End

	oTable1 = oTables.getByName("DataPilot1")  'oTables(0)
	oTable2 = oTables.getByName("DataPilot2")  'oTables(1)
Rem	Xray oTable2: End

	Dim oFilterDsc1 As Object, oFilterDsc2 As Object
	oFilterDsc1 = oTable1.FilterDescriptor
	oFilterDsc2 = oTable2.FilterDescriptor

	With oFilterDsc1
		oFilterDsc2.FilterFields = .FilterFields
	End With
	oTable2.refresh

	Exit Sub
	
HandleErrors:
	Msgbox "Error" & Err & ": " & Error _
	 , MB_ICONSTOP, "macro:SyncDataPilotsByFilter"
End Sub

This instruction does all the work:
oFilterDsc2.FilterFields = oFilterDsc1.FilterFields

Suggest any listener connection code.
In this case, the standard filter will replace the slicer.
Connecting a new table will not be difficult either. The code is simple.

You can use addModifyListener.

I tried it. Error occured.
oTable1.addModifyListener(oEventListener)
Expects 0 args. Why?!

Global oEventListener As Object

Sub StartEventListener()
	Dim oTable1 As Object
	oTable1 = ThisComponent.Sheets.getByName("PivotTables").DataPilotTables.getByName("DataPilot1")

	If IsNull(oEventListener) Then  'just to be shure it doesn't start twice
		oEventListener = CreateUnoListener("EventListener_", "com.sun.star.lang.XEventListener")	
		oTable1.addModifyListener(oEventListener)
	End If
End Sub

Sub StopEventListener()
	Dim oTable1 As Object
	oTable1 = ThisComponent.Sheets.getByName("PivotTables").DataPilotTables.getByName("DataPilot1")

	If Not IsNull(oEventListener) Then  'only if still running
		oTable2.removeModifyListener(oEventListener)
		oEventListener = Nothing  'to know later the listener has stopt
	End If
End Sub

Sub EventListener_notifyEvent(oEvent As Object) 
	Print oEvent.EventName
	Call SyncDataPilotsByFilter
End Sub

Sub EventListener_disposing(oEvent)
	MsgBox "EventListener_disposing does work now !?"
	Call StopEventListener
End Sub

Name of event is modified, type of Event is EventObject. This struct has one field: Source.

Error in line:
oTable1.addModifyListener(oEventListener)

Снимок экрана от 2021-11-22 20-05-24

We are waiting for help. And so everything works and just connects.

See above.

oEventListener = CreateUnoListener("EventListener_", "com.sun.star.util.XModifyListener")	
Sub EventListener_modified(oEvent)
 ' ...
End Sub

@sokol92 or anybody, look at the file and download a working version please.

Corrected. Well, my error disapeared, but Calc crashes…

Global oEventListener As Object

Sub StartEventListener()
	Dim oTable1 As Object
	oTable1 = ThisComponent.Sheets.getByName("PivotTables").DataPilotTables.getByName("DataPilot1")

	If IsNull(oEventListener) Then  'just to be shure it doesn't start twice
		oEventListener = CreateUnoListener("EventListener_", "com.sun.star.util.XModifyListener")	
		oTable1.addModifyListener(oEventListener)
	End If
End Sub

Sub StopEventListener()
	Dim oTable1 As Object
	oTable1 = ThisComponent.Sheets.getByName("PivotTables").DataPilotTables.getByName("DataPilot1")

	If Not IsNull(oEventListener) Then  'only if still running
		oTable1.removeModifyListener(oEventListener)
		oEventListener = Nothing  'to know later the listener has stopt
	End If
End Sub

Sub EventListener_queryInterface(oEvent As Object)
End Sub

Sub EventListener_disposing(oEvent)
	MsgBox "EventListener_disposing does work now !?"
	Call StopEventListener
End Sub

Sub EventListener_modified(oEvent As Object) 
	Call SyncDataPilotsByFilter
End Sub

File attached, updated:
2pivots-connected-to-filter.ods (20.7 KB)

Replace in EventListener_modified body to

Msgbox "Pivot table modified"

If changing the pivot table works then the problem is in SyncDataPilotsByFilter.

SyncDataPilotsByFilter works when called manually.
Yes, the listener works, but when SyncDataPilotsByFilter is called from EventListener_modified event, a crash occurs. What can be done?

UPDATED:
And we also need to find where the names of the events are hidden in order to skip those that we do not need here.

I now have no opportunity for a detailed analysis.
Perhaps this is a bug. Try to refresh the second pivot table in the EventListener_modified procedure without any modification.

There is another problem. When I manually sync the second table by SyncDataPilotsByFilter, the EventListener_modified event of the first table fires. Why?

Or the same. :slightly_smiling_face:

@kesu, you can consider another cool way to connect multiple pivot tables to a common data source and filter them at the same time.
There is a dataset. Above it is a range of Advanced Filter criteria. By the way, you can position the criteria range anywhere. You can filter the data even better here because the OR operator is also supported (only AND in pivot tables*). The filtered data is copied to another location (Copy results to output range) on the hidden sheet. And all the pivot tables are connected to this output range. Data is filtered as input to pivot tables, and tables always display complete data.
Works great!
But we need to automate a little. Very little. If this is what you need…
_______
* However, the Standard filter can do that too. This is not in Excel, just as there is no possibility to use regular expressions.

UPDATED:
If there is time and people willing to help the author with this task, I will give one more tip.
The programming of this task will become much easier if create a database range instead of a named range and use structured references to table, headers, columns, etc. (i.e. references in square brackets, who knows). This greatly simplifies the task: nothing needs to be calculated. It doesn’t matter that they are then converted to absolute references.

By the way, in Excel, this method is used just then to use the OR operator in pivot tables. But why don’t we connect multiple tables to the data source for our purposes?

https://forum.openoffice.org/en/forum/download/file.php?id=31296 is the database PowerFilter.odb from the topic I have linked on Nov 21 2021. Register the database as “PowerFilter”.
Open the attached spreadheet. I has 4 lines of macro code refreshing the pivots.
Play with the filter form.
PowerFilter_JoinPivots.ods (23.9 KB)

P.S. there is a date formatting problem I don’t care about yet. Works fine with OpenOffice.
P.P.S. LO crashes when I activate the second sheet of the original spreadsheet https://forum.openoffice.org/en/forum/download/file.php?id=31689 just another LO regression with database context.