Bach composed the cantata in 1731 for Ratswechsel, the inauguration of the new town council, which was celebrated annually in a festive service in the Nikolaikirche on the Monday following the feast day of St. Bartholomew (24 August).
It was not a democratic election, but a "ceremonial transfer of office" of council members who were appointed.
The service was not part of the liturgical year with cantata texts related to prescribed biblical epistle and gospel readings.
For the same occasion, Bach had already written the cantata Preise, Jerusalem, den Herrn, BWV 119, in his first year in Leipzig, 1723.
For the Ratswechsel service, he could count on the entire council (his employer) listening, probably also civil servants and representatives of the Elector's administration for the region.
The musicologist Klaus Hofmann notes: "It was an opportunity for Bach to show how sacred music was flourishing under his direction and to present himself as a composer."

The cantata text of Wir danken dir, Gott by an unknown author includes in movement 2 the first verse of Psalm 75 (Psalm 75:1) and as the closing chorale the fifth stanza of Johann Gramann's hymn of praise "Nun lob, mein Seel, den Herren" (1540).

Bach first performed the cantata on 27 August 1731.
In 1733, he adapted the music of the first choral movement with only minor changes for the Gratias agimus tibi of his Kyrie-Gloria Mass for the Dresden court, on a text expressing the same idea in Latin.
According to Hofmann, the movement is based on an earlier lost composition.

Bach performed the cantata for Ratswechsel at least two more times, on 31 August 1739 and on 24 August 1749.
 He expanded the Missa of 1733 to his Mass in B minor and concluded his work by repeating the music as the Dona nobis pacem.
Bach composed the cantata in 1731 for Ratswechsel, the inauguration of the new town council, which was celebrated annually in a festive service in the Nikolaikirche on the Monday following the feast day of St. Bartholomew (24 August). It was not a democratic election, but a "ceremonial transfer of office" of council members who were appointed. The service was not part of the liturgical year with cantata texts related to prescribed biblical epistle and gospel readings. For the same occasion, Bach had already written the cantata Preise, Jerusalem, den Herrn, BWV 119, in his first year in Leipzig, 1723. For the Ratswechsel service, he could count on the entire council (his employer) listening, probably also civil servants and representatives of the Elector's administration for the region. The musicologist Klaus Hofmann notes: "It was an opportunity for Bach to show how sacred music was flourishing under his direction and to present himself as a composer."
The cantata text of Wir danken dir, Gott by an unknown author includes in movement 2 the first verse of Psalm 75 (Psalm 75:1) and as the closing chorale the fifth stanza of Johann Gramann's hymn of praise "Nun lob, mein Seel, den Herren" (1540).
Bach first performed the cantata on 27 August 1731. In 1733, he adapted the music of the first choral movement with only minor changes for the Gratias agimus tibi of his Kyrie-Gloria Mass for the Dresden court, on a text expressing the same idea in Latin. According to Hofmann, the movement is based on an earlier lost composition.
Bach performed the cantata for Ratswechsel at least two more times, on 31 August 1739 and on 24 August 1749.  He expanded the Missa of 1733 to his Mass in B minor and concluded his work by repeating the music as the Dona nobis pacem.
Wir danken dir, Gott, wir danken dir, BWV 29