(Recast, adds details and context)
BRASILIA, Aug 31 (Reuters) - Brazil posted its biggest ever primary budget deficit for the month of July as revenues continue to drop, but still managed to beat market expectations for a much wider shortfall.
The country, struggling with a two-year recession, posted a primary deficit of 12.816 billion reais ($3.97 billion) in July, smaller than the 20 billion reais shortfall expected by the market.
Still, the monthly deficit is the sixth so far this year, which points to the challenges the government faces to meet its fiscal goal.
In the 12 months through July, the primary deficit was equivalent to 2.54 percent of gross domestic product, a world away from the north of 3 percent surpluses recorded just a few years ago.
The government set a primary deficit goal of 163.9 billion reais, or the equivalent of 2.6 percent of GDP.
Interim President Michel Temer has vowed to rebalance the depleted government accounts by capping public spending and reducing pension benefits.
The rapid deterioration of the public accounts after years of heavy spending and hefty tax breaks cost Brazil its coveted investment-grade rating.
$1 = 3.2271 Brazilian reais Reporting by Marcela Ayres; Writing by Alonso Soto; Editing by Chizu Nomiyama